Hello all,

I’m back ! Hope you all enjoyed Christmas. Today, I will tell you how to take screen shot and save it as .jpg, .png, .gif ( and so on ) file using java program.

Rectangle rectangle = new Rectangle(x, y, width, height);

// x = pixel from left

// y = pixel from top

// width = image width

// height = image height

Robot robot = new Robot();

BufferedImage image = robot.createScreenCapture(rectangle);

// Creates an image containing pixels read from the screen. This image does not include the mouse cursor and return BufferedImage object.

File file = new File(“screenshot.png”);

ImageIO.write(image, “png”, file);

// A class containing static convenience methods for locating ImageReaders and ImageWriters, and performing simple encoding and decoding.

//The code above will take screenshot according to rectangle object and it will save screenshot.png file in same folder where your java file is located.

//You can use Toolkit.getDefaultToolkit().getScreenSize() to take complete screenshot.

Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

Rectangle rectangle = new Rectangle(0, 0, size.width, size.height);

Its done! 😀