Java Screen Shot Code

From Simson Garfinkel
Jump to navigation Jump to search

Make a Screen Shot with Java

import java.awt.image.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;

public class ScreenShot
{
    public static void main(String[] args)
    {
	try
	    {
		// Get the screen size
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle rectangle = new Rectangle(0, 0, screenSize.width, screenSize.height);
		Robot robot = new Robot();
		BufferedImage image = robot.createScreenCapture(rectangle);
		File file;

		// Save the screenshot as a png
		file = new File("screen.png");
		ImageIO.write(image, "png", file);

	    } catch (Exception e) {
		System.out.println(e.getMessage());
	    }
    }
}