Copy image to clipboard

From Processing

Jump to: navigation, search
Versions: 1.0+
Contributors: winne
Started: 2009-03-02


It's often desirable to copy the (intermediate) result of a processing sketch to the clipboard for further use in presentations, manuals etc.

Source Code

To copy the current state of your processing sketch to the clipboard, you can use the following few lines of code:

BufferedImage clipBoardImage =
  new BufferedImage(width, height,
    (g.format == ARGB) ? BufferedImage.TYPE_INT_ARGB :
      BufferedImage.TYPE_INT_RGB);
g.loadPixels();
clipBoardImage.setRGB(0, 0, width, height, g.pixels, 0, width);
ImageSelection.copyImageToClipboard(clipBoardImage);

The ImageSelection class can be found at http://elliotth.blogspot.com/2005/09/copying-images-to-clipboard-with-java.html

If you want to initiate your copy operation by CTRL+C, you can embed the code above inside the keyReleased function:

void keyReleased() {
  // CTRL+C: Copy to Clipboard
  if (key == 3) {
    // put "copy to clipboard" code here
  }
}

Please note for cross platform applications, that MacOS uses Alt+C for the same operation.

If you want to clip your image (e.g., omit status bar in your copy), you can also use different width and height coordinates:

// copy (0, 0, otherWidth, otherHeight) region to clipboard
BufferedImage clipBoardImage =
  new BufferedImage(otherWidth, otherHeight,
    (g.format == ARGB) ? BufferedImage.TYPE_INT_ARGB :
      BufferedImage.TYPE_INT_RGB);
g.loadPixels();
// important: the last parameter really has to be the original width!
clipBoardImage.setRGB(0, 0, otherWidth, otherHeight, g.pixels, 0, width);
ImageSelection.copyImageToClipboard(clipBoardImage);

Downloads

Related Links

Personal tools