Created
September 6, 2020 17:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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