How to take a screenshot in Java?

Java

Java Problem Overview


> Possible Duplicate:
> Is there a way to take a screenshot using Java and save it to some sort of image?

How to take a screenshot in Java?

Java Solutions


Solution 1 - Java

Use Robot#createScreenCapture().

BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("/screenshot.png"));

Solution 2 - Java

You can find this code useful. this code will take screenshot in every 10 seconds

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;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


public class screen2image
{
	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a");

	public void robo() throws Exception
	{
		Calendar now = Calendar.getInstance();
		Robot robot = new Robot();
		BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(screenShot, "JPG", new File("d:\\"+formatter.format(now.getTime())+".jpg"));
		System.out.println(formatter.format(now.getTime()));
	}

	public static void main(String[] args) throws Exception
	{
		screen2image s2i = new screen2image();
		while(true)
		{
			s2i.robo();
			Thread.sleep(10000);
		}
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdesaraView Question on Stackoverflow
Solution 1 - JavaBalusCView Answer on Stackoverflow
Solution 2 - JavaRaj GuptaView Answer on Stackoverflow