I'm using JNA / CGWindowListCreateImage to take a screenshot on an OS X system.
The code is mostly taken from this question:
Invalid memory access of location with Rococoa
My problem is that although it "works" it does take a copy of an entire desktop, whose size changes depending on the position of other windows (for example if another window is partially onscreen, then the part that is not onscreen will contribute to make the "desktop" screenshot larger).
So on a 1680x1050 I get "desktop screenshot" (well, not really), that can be 1714x1084 pixels etc.
I take it that by passing the correct origin / bounds my problem shall go away but I don't know how to get that info, especially not using JNA.
Here's basically what I've got:
final int windowIdDesktop = 0;
QuartzLibrary.CGRect bounds = new QuartzLibrary.CGRect.CGRectByValue();
bounds.origin = new QuartzLibrary.CGPoint();
bounds.origin.x = 0;
bounds.origin.y = 0;
bounds.size = new QuartzLibrary.CGSize();
bounds.size.width = 0;
bounds.size.height = 0;
ID imageRef = QuartzLibrary.INSTANCE.CGWindowListCreateImage(bounds,QuartzLibrary.kCGWindowListOptionOnScreenOnly, windowIdDesktop, QuartzLibrary.kCGWindowImageDefault);
Apparently someone who had the same issue, but in Objective-C, used the following technique to compute the correct bounds:
the benefit of the archives and anyone else having a similar issue, here is how I calculate the bounding box:
NSRect desktopRect = NSZeroRect; for (NSScreen *screen in [NSScreen screens]) { desktopRect = NSUnionRect(desktopRect, [screen frame]); }
Why is this necessary and how can I translate this to JNA? All I need to know is what the origin of the visible portion of the screen is (at least I think so)
The CGWindowListCreateImage states the following:
Setting
`screenBounds' to `CGRectInfinite' will include all the windows on the
entire desktop. Setting `screenBounds' to `CGRectNull' will use the
bounding box of the specified windows as the screen space rectangle.
But I really don't know how I can "simulate" or pass either CGRectInfinite nor CGRectNull to my JNA code. I'm not even sure it's the way to go.