NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
I noticed that when I use the code suggested in several past emails, as shown below, I get an JPEG image file that is the right size, but only a black image is written. import com.sun.image.codec.jpeg.*; ... //this is run in its own thread... DisplayImpl display = ...; ... BufferedImage image = display.getImage(); JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(image); param.setQuality(1.0f, true); FileOutputStream fout = new FileOutputStream(f); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fout); encoder.encode(image, param); fout.close(); I am using jdk1.4 on W2K platform, but I also tried it with jdk1.3 and the same problem occurs. If instead I use the following with jdk1.4, I can paste to the clipboard and the image appears fine in other programs (i.e. Word, PowerPoint, etc.). //this is run in its own thread... BufferedImage image = display.getImage(); Icon icon = new ImageIcon(image); final JLabel label = new JLabel(icon); label.setTransferHandler(new ImageSelection()); Toolkit kit = Toolkit.getDefaultToolkit(); final Clipboard clipboard = kit.getSystemClipboard(); TransferHandler handler = label.getTransferHandler(); handler.exportToClipboard(label, clipboard, TransferHandler.COPY); jkd1.4 has a new ImageIO class, which simplifies the top example to a single line: import javax.imageio.ImageIO; BufferedImage image = ...; File file = ...; ImageIO.write(image, "JPEG", file); However, this doesn't work either. I checked a few newsgroups, and I think it may be a JPEG encoding error in the Sun code. What does work well is: BufferedImage image = ...; File file = ...; ImageIO.write(image, "PNG", file); (I prefer PNG over JPEG's anyway, since I use GIMP for image rendering) Hopefully this may save a few of you some time, if you have the luxury of using java 1.4. I haven't yet checked if other image formats work okay with the ImageIO class Steve Potts, Ph.D. Informatics Research Scientist SurroMed Mountain View, CA spotts@xxxxxxxxxxxx (650) 230-1822
visad
archives: