Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to take a screenshot using Java and save it to some sort of image?

Simple as the title states: Can you use only Java commands to take a screenshot and save it? Or, do I need to use an OS specific program to take the screenshot and then grab it off the clipboard?

like image 768
jjnguy Avatar asked Sep 12 '08 04:09

jjnguy


People also ask

How do you take a screenshot in Java?

Taking a Screenshot Using RobotThe dimensions of the screen are accessible through the Toolkit class by using its getScreenSize() method. On systems with multiple screens, the primary display is used by default. After capturing the screen into BufferedImage, we can write it to the file with ImageIO. write().

How do I take a screenshot of a particular shape?

Click Start, then type “snipping tool” into the search box. Select Snipping Tool from the results. Click Mode to select the type of screen grab you want to take. Then use your mouse to select the area of your screen or window you want to capture.


1 Answers

Believe it or not, you can actually use java.awt.Robot to "create an image containing pixels read from the screen." You can then write that image to a file on disk.

I just tried it, and the whole thing ends up like:

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage capture = new Robot().createScreenCapture(screenRect); ImageIO.write(capture, "bmp", new File(args[0])); 

NOTE: This will only capture the primary monitor. See GraphicsConfiguration for multi-monitor support.

like image 189
David Citron Avatar answered Sep 23 '22 17:09

David Citron