Posted By

visudex on 11/18/10


Tagged

image png java screen capture shot robot buffered


Versions (?)

Screen Shot in Java and saves as PNG


 / Published in: Java
 

This snippet will allow you to capture your screen in Java and saves the image as a PNG

  1. import java.awt.AWTException;
  2. import java.awt.Robot;
  3. import java.awt.Rectangle;
  4. import java.awt.Toolkit;
  5. import java.awt.image.BufferedImage;
  6. import java.io.*;
  7. import javax.imageio.ImageIO;
  8.  
  9. class ScreenCapture {
  10. public static void main(String args[]) throws AWTException, IOException {
  11. // Captures the image from the screen
  12. BufferedImage screencapture = new Robot().createScreenCapture(
  13. new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); //Gets the size of the screen
  14.  
  15. // Writes the File as a PNG
  16. File file = new File("screencapture.png");//Saves your file in the project folder
  17. ImageIO.write(screencapture, "png", file);
  18. }
  19. }

Report this snippet  

You need to login to post a comment.