Advertisement
mahldcat

ScreenCapture.java

Sep 8th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import javax.imageio.ImageIO;
  5.  
  6. public class ScreenCapture {
  7.  
  8.     public static void captureScreen(String fileName) throws Exception {
  9.         // Create a Robot instance for capturing screen
  10.         Robot robot = new Robot();
  11.  
  12.         // Define the area of the screen you want to capture
  13.         Rectangle captureRect = new Rectangle(200, 200, 500, 300);  // Capture area
  14.  
  15.         // Capture the defined part of the screen
  16.         BufferedImage capturedImage = robot.createScreenCapture(captureRect);
  17.  
  18.         // Save the captured image as a PNG file
  19.         File file = new File(fileName);
  20.         ImageIO.write(capturedImage, "PNG", file);
  21.  
  22.         System.out.println("A partial screenshot was saved!");
  23.     }
  24.  
  25.     public static void main(String[] args) {
  26.         try {
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement