Tuesday, October 19, 2021

Capture Screenshot in Java

Sometimes you just need to capture a screenshot in Java. Well look no further here's the code in question!


import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScreenShot {

public static void main(String[] args) throws IOException {
BufferedImage image;
try {
image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("screenshot.png"));
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment

A Placebo?

 At times I wonder if my medication is just a placebo. That is a medication that usually has no effect on a disease. Am I dreaming it all up...