1

Currently I am developing a project and it is a requirement that I screenshot the current active window on the screen (Assuming one monitor) and save it as an image.

I have worked at the following code which screenshots the entire screen:

int x = 0,y = 0;
Color suit = new Robot().getPixelColor(x, y);
Rectangle fs = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage rank = new Robot().createScreenCapture(fs);
ImageIO.write(rank, "bmp", new File("hi.bmp"));

and I am of the understanding that to get the size of the current active window one must use a method as such:

public static long getHWnd(Frame f) {
    return f.getPeer() != null ? ((WComponentPeer) f.getPeer()).getHWnd() : 0;
}

however I am having trouble implementing this method into my code, and I have no previous experience working with frames or rectangles.

Could I be pointed in the right direction in terms of where to go next! Thanks.

luator
  • 4,769
  • 3
  • 30
  • 51
  • 1
    Just out of curiosity--you can't leave that feature to the OS? All the major OSs have that feature and there's no sense reinventing the wheel... – Alvin Thompson Jun 14 '15 at 23:53
  • I understand that but in this specific case, the screen must be scraped – user3078490 Jun 14 '15 at 23:55
  • Maybe something like [this](http://stackoverflow.com/questions/6391439/getting-active-window-information-in-java) or [this](http://stackoverflow.com/questions/5767104/using-jna-to-get-getforegroundwindow) might help – MadProgrammer Jun 14 '15 at 23:57
  • Your `getHWnd` method will only work for a `java.awt.Frame` or child thereof. Is the window you're trying to capture a AWT based window? If so, you can just it's location and size via it's API... – MadProgrammer Jun 14 '15 at 23:58

0 Answers0