Choose Your Language

Thursday 27 June 2013

How to capture screenshot and save it to a file using Java

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.util.Date;
public class ScreenShot {
    public static void main(String a[]) throws Exception{
        ScreenShot screenShot=new ScreenShot ();
        Date date=new Date();
        String time=String.valueOf(date.getTime());
        String fileName="C:\\folderName\\screenshot"+time+".png";
        screenShot.captureScreen(fileName);
    }
   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));

}
}

No comments:

Post a Comment