"I Think, I Conceptualize, I Analyze, I Design, and I Create." ~ Puneet Kalra

Cognitive Robotics Research Centre Of University Of Wales, Newport Puneet Kalra

Home Studies Research Projects Tutorials Portfolio

Puneet Kalra - www.puneetk.com - Socializing Robots

Screen Shot Using Java

June 29th, 2009 by Puneet Kalra Leave a reply »

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! :D

One Response

  1. SonyaSunny says:

    Hi, puneetk – da best. Keep it going!
    Thank you
    SonyaSunny

Leave a Reply

Please note : I will help you only if you will show some efforts! Don't expect replies to simple queries, You can easily find answers for them through searching.

I'm a student and I work as well. So i might reply late here. If you have something important to discuss Or stuck somewhere in your project. Please post on my Facebook page for a quick response.