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.

Screen Capture: A part of "Gooey Beans, a trilogy in 42 parts"

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

Resources - where i will collate these articles and their code.

Related Topics >>