Here you can find the source of doScreenshotToFile(final Component container, final Path filePath, final String imageType)
public static File doScreenshotToFile(final Component container, final Path filePath, final String imageType) throws IOException
//package com.java2s; //License from project: Open Source License import java.awt.Component; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.Transparency; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.nio.file.Path; import javax.imageio.ImageIO; public class Main { private final static GraphicsConfiguration GC = (GraphicsEnvironment.isHeadless() == false) ? GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration() : null;// w w w .j a v a 2s . c om public static File doScreenshotToFile(final Component container, final Path filePath, final String imageType) throws IOException { final File imageFile = new File(filePath.toString()); ImageIO.write(getScreenshotImage(container), imageType, imageFile); return imageFile; } /** * Creates an image of the contents of container and saves to file. */ public static File doScreenshotToFile(final Component container, final Path filePath) throws IOException { return doScreenshotToFile(container, filePath, "png"); } private static BufferedImage getScreenshotImage(final Component container) { final Rectangle rec = container.getBounds(); final BufferedImage capture = getCompatibleBufferedImage(rec.width, rec.height); container.paint(capture.getGraphics()); return capture; } public static BufferedImage getCompatibleBufferedImage(final int width, final int height, final int transparency) { if (GC != null) { return GC.createCompatibleImage(width, height, transparency); } else { final int type = (transparency == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; return new BufferedImage(width, height, type); } } public static BufferedImage getCompatibleBufferedImage(final int width, final int height) { return getCompatibleBufferedImage(width, height, Transparency.OPAQUE); } public static BufferedImage getCompatibleBufferedImage(BufferedImage image) { return getCompatibleBufferedImage(image.getWidth(), image.getHeight(), image.getTransparency()); } }