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)
See this site http://www.screencast-o-matic.com/
It uses the Robot class to create the screen cast
Thanks Suresh for the link.
Great!!!!!
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…………….
Change it to :
g.drawImage(im, 0, 0, p, this);
Eclipse showed major errors it ddint work at all..
Class – thankx
How to save file after taking screenshot in a specific path like….
D:\New Folder
Thank you
I would like to know how can I take screenshots randomly using the same coding.
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 –
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.
Hey.. Bhaskar , Have you resolved this ( black screen) issue in windows 7 when you code running as windows service. I am facing same issue.
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();
}
}
awesome
How to keep it alive to take span shot for specific area using mouse drag and drop
Kindly suggest if i want to take screenshots from the clients end using the web application
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
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
I want to take screenshot of a folder which is present in different directory. help me
I tried this but it is capturing black screenshot in case I log off the system or execution is happening on Jenkins.