1

I have some code that can take a screenshot of the desktop to get my an image, My goal is to have a program that will take specific screenshots of specific programs, and I'm wondering if there is a way to get the dimensions and locations of specific windows/programs, im looking through java.awt and wondering if im looking in the correct locations.

public class Screenshot {
    
    public static BufferedImage capture;
    
    
    public static void main(String[] args){
        File outputfile = new File("saved2.png");
        
        
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        
        
        try {
            capture = new Robot().createScreenCapture(screenRect);
            
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        // Writes out to a file
        // https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html
        try {
            ImageIO.write(capture, "png", outputfile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.print("Screenshot captured");
    }
}
Carl
  • 11
  • 1
  • 1
    The short answer is no, there's no way to get the window boundaries of any window which wasn't launched within the current JVM through the Java API. If you want to do this for windows not launched in the current JVM, you need to investigate a native solution, possibly using JNI/JNA – MadProgrammer May 05 '21 at 21:36
  • 1
    For a [native example](https://stackoverflow.com/questions/8717999/how-to-get-list-of-all-window-handles-in-java-using-jna) – MadProgrammer May 05 '21 at 21:37

0 Answers0