Screen Shot in Java and saves as PNG


/ Published in: Java
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.