BufferedImage createScreenCapture(Rectangle screenRect)
We pass a screen region (in rectangle) to be captured as this method’s parameter. The captured graphic is returned as a BufferedImageobject which can be drawn onto a GUI component or saved to a file. And we use the ImageIO.write() static method to save the BufferedImage object to a file:boolean write(RenderedImage im, String formatName, File output)
where the formatName can be “jpg”, “png”, “bmp”, etc.NOTE: The captured image does not include the mouse cursor.Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());And the following program captures a full screenshot and saves it to a JPG image file:
package net.codejava.graphics; import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * This program demonstrates how to capture a screenshot (full screen) * as an image which will be saved into a file. * @author www.codejava.net * */ public class FullScreenCaptureExample { public static void main(String[] args) { try { Robot robot = new Robot(); String format = "jpg"; String fileName = "FullScreenshot." + format; Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage screenFullImage = robot.createScreenCapture(screenRect); ImageIO.write(screenFullImage, format, new File(fileName)); System.out.println("A full screenshot saved!"); } catch (AWTException | IOException ex) { System.err.println(ex); } } }
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2);And following code is for a complete program:
package net.codejava.graphics; import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; /** * This program demonstrates how to capture screenshot of a portion of screen. * @author www.codejava.net * */ public class PartialScreenCaptureExample { public static void main(String[] args) { try { Robot robot = new Robot(); String format = "jpg"; String fileName = "PartialScreenshot." + format; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Rectangle captureRect = new Rectangle(0, 0, screenSize.width / 2, screenSize.height / 2); BufferedImage screenFullImage = robot.createScreenCapture(captureRect); ImageIO.write(screenFullImage, format, new File(fileName)); System.out.println("A partial screenshot saved!"); } catch (AWTException | IOException ex) { System.err.println(ex); } } }The image file (PartialScreenshot.jpg) is stored under the program’s current directory.
void captureComponent(Component component) { Rectangle rect = component.getBounds(); try { String format = "png"; String fileName = component.getName() + "." + format; BufferedImage captureImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB); component.paint(captureImage.getGraphics()); ImageIO.write(captureImage, format, new File(fileName)); System.out.printf("The screenshot of %s was saved!", component.getName()); } catch (IOException ex) { System.err.println(ex); } }Run the demo program (ComponentGUICaptureExample.java in the attachments section):Click the Capture Me! button, its image will be saved as the button1.png file which looks like this:And click the Capture This Frame! button, image of the frame’s content will be saved as the frame0.png file which looks like this: