How to take screen shots in Java

how to take screenshots in java

While surfing through internet, I came to this amazing piece of code in Java that takes the screen shot of your desktop and save it in a PNG file.

This example uses java.awt.Robot class to capture the screen pixels and returns a BufferedImage. Java.awt.Robot class is used to take the control of mouse and keyboard. Once you get the control, you can do any type of operation related to mouse and keyboard through your java code. This class is used generally for test automation.

Copy and paste following code in your Java class and invoke the method captureScreen() with file name as argument. The screen shot will be stored in the file that you specified in argument.

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

...

public void captureScreen(String fileName) throws Exception {

   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   Rectangle screenRectangle = new Rectangle(screenSize);
   Robot robot = new Robot();
   BufferedImage image = robot.createScreenCapture(screenRectangle);
   ImageIO.write(image, "png", new File(fileName));

}
...


17 Comments

  • Suresh G 16 February, 2009, 10:33

    See this site http://www.screencast-o-matic.com/
    It uses the Robot class to create the screen cast

  • Viral Patel 18 February, 2009, 14:45

    Thanks Suresh for the link.

  • Chetan 25 March, 2010, 12:28

    Great!!!!!

  • prashant ingale 24 December, 2011, 21:18

    i have managed to take screenshot but i am failing to display it . It gives nullpointerexception for following code
    JPanel p=new JPanel();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);
    ImageIcon img=(ImageIcon)image;
    Image im=img.getImage();
    Graphics g;
    g.drawImage(im,0,0,p);

    plz help me…………….

  • Josh 18 March, 2012, 3:13

    Change it to :

    g.drawImage(im, 0, 0, p, this);

  • Wes 20 April, 2012, 6:28

    Eclipse showed major errors it ddint work at all..

  • kevein 29 June, 2012, 15:37

    Class – thankx

  • ramit das 11 March, 2013, 14:49

    How to save file after taking screenshot in a specific path like….
    D:\New Folder

  • Habi 21 March, 2013, 21:59

    Thank you

  • Basil Roy 4 April, 2013, 14:48

    I would like to know how can I take screenshots randomly using the same coding.

  • Bhaskar 25 June, 2013, 17:18

    Hi:

    I have created a JAVA class whose work is to take screenshot in every 5 min for computer screen its running on and save it in a folder..

    Below is code I am using to get it work –

    BufferedImage screenImage = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    
    ImageIO.write(robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())), "gif", new File(location+(cal.get(Calendar.HOUR)+"_"+cal.get(Calendar.MINUTE)+"_"+counter)+".gif"));
    

    This code works fine when I run on command prompt or creting a JAR.

    But when I try to run it from Windows task scheduler or Windows START UP it works but save screen shot images with all black in color.

    Can anyone help in this I tired googled but I found nothing.

    • dheeraj 21 April, 2014, 16:28

      Hey.. Bhaskar , Have you resolved this ( black screen) issue in windows 7 when you code running as windows service. I am facing same issue.

  • Ace 30 October, 2013, 18:08

    its show error

    import java.awt.Dimension;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;

    public class screenshot
    {
    static String myfile = “D:/img.png”;
    public static void main(String[] args)
    {

    System.out.println(“Hello, World!”);
    screenshot s = new screenshot();
    s.captureScreen(myfile);
    }
    public void captureScreen(String fileName) throws Exception {
    try{

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle screenRectangle = new Rectangle(screenSize);
    Robot robot = new Robot();
    BufferedImage image = robot.createScreenCapture(screenRectangle);
    ImageIO.write(image, “png”, new File(fileName));
    }
    catch (IOException e)
    {

    e.printStackTrace();
    }

    }

  • nishan 31 October, 2013, 17:53

    awesome
    How to keep it alive to take span shot for specific area using mouse drag and drop

  • paul 30 December, 2013, 12:13

    Kindly suggest if i want to take screenshots from the clients end using the web application

  • pvs chaudhary 16 July, 2014, 16:36

    Hey, i

    i am using this code in servlet and JSP, its work properly but when i create WAR file an deploy it on Tomcat server then it take BLACK Screen image,

    how it take proper screen of client machine.
    kindly help me

    thanks

  • Sneha Latha 30 October, 2014, 12:59

    Awesome post…….very helpful for me, I faced the black screen issue with Selenium Screenshot methodology, i surfed so many sites for solution, fortunately, i came across this blog, its capturing perfectly :) tq so much viral

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]