Skip to main content

Screen Capture

Posted by evanx on March 28, 2007 at 6:52 AM PDT

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.

href="https://aptframework.dev.java.net/gooey/screenCapture.html">
align="left" hspace="8"/>

Click here to read "Screen Capture"

A part of "Gooey Beans, the GUI part of a trilogy in 42 parts"

style="text-decoration: none;">



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));
    }


style="text-decoration: none;">


style="text-decoration: none;">
style="text-decoration: none;">
Related Topics >>