1.11M Members

Java code to capture your screen as image

 
0
 

The code simply captures your working window as jpeg image and stores it in the same directory as where your java class after compiling by javac is being stored

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
 
import javax.imageio.ImageIO;
 
public class screen2image
{
	public static void main(String[] args) throws Exception
	{
		Robot robot = new Robot();
 
		BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
	}
}
 
0
 

does this small code make such a lot anyway thnks

 
0
 
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class screen2image
{
    public static void main(String[] args) throws Exception
    {
        Robot robot = new Robot();

        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    }
}
 
0
 
import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.image.BufferedImage;import java.io.File; import javax.imageio.ImageIO; public class screen2image{   public static void main(String[] args) throws Exception {       Robot robot = new Robot();      BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));       ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));   }}import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class screen2image
{
    public static void main(String[] args) throws Exception
    {
        Robot robot = new Robot();

        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    }
}
 
0
 

hi all, is there any way of getting mouse location on screen even it is out of frame? normally e.getLocationOnScreen() method works but out of frame.
can i use Rectangle to do that?

 
0
 

Make a new thread. Ask your question in it.

 
0
 

how does this thing suppose to work? cos i have netbeans as development environment on my vista os,i have inputed the codes and it indicted its with no error on my template,i even run it and it run fine, but how it works is what i don't understand so how do i know how it captures my screen and where does it store it?

 
-1
 

Thank you for sharing the code...
But will this small code do this big thing,,,

 
0
 

Capturing a screenshot may be achieved in 2 ways -
1. using "robot.createScreenCapture()", as mentioned above.
2. using "robot.keyPress()" to simulate hitting PrtScr key (for most systems).
method 2 may be something like this:

    r.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);

you might want to combine these methods, testing the success of one, and trying the other if the 1st one failed.

Capturing part of the screen:
1.
using 1st method: "robot.createScreenCapture(bounds)", where bounds might be a window's rectangle, for example:

    Rectangle bounds = frame.getBounds()

2.
using the 2nd method to hit ALT+PrtScr, to capture only the currently focused window:

    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
    robot.keyRelease(KeyEvent.VK_ALT);
 
0
 

how to save the image after simulating alt+prtscr by above code??

 
0
 

do you have any idea how old this thread is?

You
Post:
Start New Discussion
Tags Related to this Article