Skip to content

Instantly share code, notes, and snippets.

@antic-ml
Created September 6, 2020 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antic-ml/3d8596a8b91f81cd72e129a5b125dbf8 to your computer and use it in GitHub Desktop.
Save antic-ml/3d8596a8b91f81cd72e129a5b125dbf8 to your computer and use it in GitHub Desktop.
Java program to capture the screen and save it out as a PNG
/**
* Grabs a shot of the screen and saves it out as a PNG file.
*/
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class Screenshot {
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
try {
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File("screen.png"));
} catch(AWTException awte) {
System.out.println("AWTException: "+awte.getMessage());
} catch(IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment