Java BufferedImage to clipboard gives IIOException

I am trying to copy an image (residing in a BufferedImage object) to the clipboard. I am using the code from this answer .

When I try to insert an image into the program, nothing happens. The GIMP shows a message that no image data was found in the clipboard.

I have also tried the workaround from this article . In fact, 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: every time I try to insert, 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(
        at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(
        at javax.imageio.ImageWriter.write(
        at sun.awt.datatransfer.DataTransferer.imageToStandardBytesImpl(
        at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(
        at sun.awt.X11.XDataTransferer.imageToPlatformBytes(
        at sun.awt.datatransfer.DataTransferer.translateTransferable(
        at sun.awt.datatransfer.DataTransferer$
        at java.awt.event.InvocationEvent.dispatch(
        at java.awt.EventQueue.dispatchEventImpl(
        at java.awt.EventQueue.access$000(
        at java.awt.EventQueue$
        at java.awt.EventQueue$
        at Method)
        at java.awt.EventQueue.dispatchEvent(
        at java.awt.EventDispatchThread.pumpOneEventForFilters(
        at java.awt.EventDispatchThread.pumpEventsForFilter(
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(
        at java.awt.EventDispatchThread.pumpEvents(
        at java.awt.EventDispatchThread.pumpEvents(


I am on Ubuntu 11.10 x64 and have tried pasting in various programs, including 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)


Does anyone face the same problems? I did something wrong, this is a Java bug, is there a workaround?

EDIT: I am using a rather messy workaround for now. I took a little Python script I found on stackoverflow, write Image to a file from Java, and load the file into this script, which copies it to the clipboard. Of course, this is everything except the platform independent. So I'm still hoping for a Java solution.

Yours, Max Weller


source to share

1 answer

You are probably using OpenJDK which does not support JPEG image writing. You can try switching to Sun / Oracle Java.



All Articles