Skip to main content

Screen Capture

Posted by evanx on March 28, 2007 at 9:52 AM EDT

For documentation purposes (and perhaps remote support), we need snapshot images of our application. So let's build such support directly into our application itself.


Code Snippets

We can capture whole desktop, and save it to a PNG file, as follows.

    public void captureScreen(String fileName) throws Exception {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenSize));
        ImageIO.write(image, "png", new File(fileName));
    }

Alternatively, we might capture our JFrame, including its window decoration, as follows.

    public void captureFrame(JFrame frame, String fileName) throws Exception {
        BufferedImage image = new Robot().createScreenCapture(frame.getBounds());
        ImageIO.write(image, "png", new File(fileName));
    }


Related Topics >>