Java code to capture your screen as image

himanjim 0 Tallied Votes 2K Views Share

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"));
	}
}
Bharath15 0 Newbie Poster

does this small code make such a lot anyway thnks

ceers 0 Newbie Poster
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"));
    }
}
Ghost_Ryder 0 Newbie Poster
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"));
    }
}
ykpemre 0 Newbie Poster

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?

BestJewSinceJC 700 Posting Maven

Make a new thread. Ask your question in it.

dhiralpandya 0 Newbie Poster

I created small Screen Capture In Java may be it is useful for you.

gyno -5 Light Poster

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?

jacksonbird03 1 Newbie Poster

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

Obfuscator 0 Newbie Poster

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);
Uday kiran_1 0 Newbie Poster

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

stultuske 1,116 Posting Maven Featured Poster

do you have any idea how old this thread is?

Ashan_1 0 Newbie Poster

its successful tnx :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.