Java examples for 2D Graphics:Image
save Image To File
import java.awt.AWTException; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.log4j.Logger; public class Main{ private static void saveImageToFile(final Image image, final File file, final String fileType) { try {/* w w w .j av a 2 s . c om*/ final BufferedImage bufferedImage = toBufferedImage(image); ImageIO.write(bufferedImage, fileType, file); } catch (final IOException e) { System.out.println("Write error for " + file.getPath() + ": " + e.getMessage()); } } private static BufferedImage toBufferedImage(final Image src) { final int w = src.getWidth(null); final int h = src.getHeight(null); final int type = BufferedImage.TYPE_INT_RGB; final BufferedImage dest = new BufferedImage(w, h, type); final Graphics2D g2 = dest.createGraphics(); g2.drawImage(src, 0, 0, null); g2.dispose(); return dest; } }