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)); } ...
Code language: Java (java)
Get our Articles via Email. Enter your email address.

You may also like...

19 Comments

  1. Suresh G says:

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

  2. Thanks Suresh for the link.

  3. Chetan says:

    Great!!!!!

  4. prashant ingale says:

    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…………….

  5. Josh says:

    Change it to :

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

  6. Wes says:

    Eclipse showed major errors it ddint work at all..

  7. kevein says:

    Class – thankx

  8. ramit das says:

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

  9. Habi says:

    Thank you

  10. Basil Roy says:

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

  11. Bhaskar says:

    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 says:

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

  12. Ace says:

    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();
    }

    }

  13. nishan says:

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

  14. paul says:

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

  15. pvs chaudhary says:

    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

  16. Sneha Latha says:

    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

  17. Alok says:

    I want to take screenshot of a folder which is present in different directory. help me

  18. Amol Suryawanshi says:

    I tried this but it is capturing black screenshot in case I log off the system or execution is happening on Jenkins.

Leave a Reply

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