Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to copy a image (held in a BufferedImage object) to the clipboard. I'm using the code from this answer .

When trying to paste the image in a program, simply nothing happens. GIMP shows a message saying no image data was found in the clipboard.

I also tried the workaround from this article . Effectively, I changed the constructor to this:

        Robot robot = new Robot();
        Dimension screenSize  = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screen = new Rectangle( screenSize );
        BufferedImage i = robot.createScreenCapture( screen );
        // ----- start of changes from workaround -----
        // Work around a Sun bug that causes a hang in "sun.awt.image.ImageRepresentation.reconstruct".
        new javax.swing.ImageIcon(i); // Force load.
        BufferedImage newImage = new BufferedImage(i.getWidth(null), i.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        newImage.createGraphics().drawImage(i, 0, 0, null);
        i = newImage;
        // ----- end of changes from workaround -----
        TransferableImage trans = new TransferableImage( i );
        Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
        c.setContents( trans, this );

This doesn't make it work, but it changes the behavior: Everytime I try to paste, my program displays the following exception in the console:

javax.imageio.IIOException: Invalid argument to native writeImage
        at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
        at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1055)
        at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:357)
        at javax.imageio.ImageWriter.write(ImageWriter.java:615)
        at sun.awt.datatransfer.DataTransferer.imageToStandardBytesImpl(DataTransferer.java:2107)
        at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:2037)
        at sun.awt.X11.XDataTransferer.imageToPlatformBytes(XDataTransferer.java:165)
        at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1277)
        at sun.awt.datatransfer.DataTransferer$6.run(DataTransferer.java:2208)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647)
        at java.awt.EventQueue.access$000(EventQueue.java:96)
        at java.awt.EventQueue$1.run(EventQueue.java:608)
        at java.awt.EventQueue$1.run(EventQueue.java:606)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:617)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

I'm on Ubuntu 11.10 x64 and tried pasting to various programs, among them LibreOffice Draw, LibreOffice Writer, GIMP, InkScape.

mw@nb999:~$ java -version
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)

EDIT : I'm using a quite dirty workaround at the moment. I took a small Python script I found on stackoverflow, write the Image to file from Java and feed the file to this script which copies it to the clipboard. Of course, this is everything but platform independent. So I'm still hoping for a solution in Java.

what is the signature of drawImage() - your args don't match any of the signatures I found with a quick look at javadocs. Have you narrowed down exactly which line of the code fails? - your stack trace doesn't go far enough into your code to tell us. – John3136 Mar 17, 2012 at 23:57 2. That's my problem - the stack trace I attached is the whole thing Java tells me. So apparently it is not directly in my code. It doesn't occur when I copy it (-> when I execute the code). But it does occur when I paste. The exception is entirely in Java VM's code. Either I give wrong parameters in some place, or the VM has a bug. – Mira Weller Mar 18, 2012 at 10:04 I also found this item in Sun's bug tracker, which describes my problem quite good. But it is from last year and I don't really understand what they mean by State: Closed, Unverified (is it fixed now?) – Mira Weller Mar 18, 2012 at 10:20 Sounds like a bug on the receiving end - from the link above, you could try copying a different image format? – John3136 Mar 18, 2012 at 10:36

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.