1,105,310 Community Members

Java code to capture your screen as image

Member Avatar
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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"));
	}
}
Member Avatar
Bharath15
Newbie Poster
2 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

does this small code make such a lot anyway thnks

Member Avatar
ceers
Newbie Poster
1 post since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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"));
    }
}
Member Avatar
Ghost_Ryder
Newbie Poster
1 post since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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"));
    }
}
Member Avatar
ykpemre
Newbie Poster
1 post since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

Make a new thread. Ask your question in it.

Member Avatar
dhiralpandya
Newbie Poster
1 post since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
gyno
Light Poster
43 posts since Sep 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
jacksonbird03
Light Poster
26 posts since May 2013
Reputation Points: 1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
-1
 

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

Member Avatar
Obfuscator
Newbie Poster
2 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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);
Member Avatar
Uday kiran_1
Newbie Poster
1 post since Feb 2014
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,317 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

do you have any idea how old this thread is?

You
Post:
Start New Discussion
Tags Related to this Article