4

I'm trying to get an image of an offscreen window to use in a CALayer-based animation, but no matter which method I try I cannot get an image out the other side. Here is the code I've been using on a custom NSWindow subclass in my project:

CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, 
                                                 kCGWindowListOptionAll, 
                                                 (CGWindowID)[self windowNumber], 
                                                 kCGWindowImageDefault);

This is what the image should look like:

alt text

and here's what I get (ignore the slightly larger size - that's my fault in the rendering of the image):

alt text

Can anyone see where I'm going wrong? Strangely, Quartz Debug can see the window image without issue, but the Son of Grab sample code from Apple cannot.

Tony Arnold
  • 2,738
  • 1
  • 23
  • 34
  • I'm wanting to capture an off screen window, too. Did you ever find an answer to this? – Sean Sep 21 '11 at 21:00
  • 1
    I'd like to confirm that CGWindowListCreateImage will not capture offscreen windows kCGWindowListOptionIncludingWindow on 10.9. – GenericPtr Jul 28 '14 at 19:29

1 Answers1

2

From the documentation comment for CGWindowListCreateImage in CGWindow.h:

kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly: Use all on-screen windows in this user session to construct the image. The parameter windowID should be kCGNullWindowID.

That's why you're getting a screenshot of all the windows that are within the rectangle you're interested in.

To take a screenshot of a specific window, use kCGWindowListOptionIncludingWindow.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Thanks Peter, I should have mentioned in my question that I tried that option as well to no avail. The interesting part of this is that the rect that has been captured is not the rect that the window is sitting in when it is onscreen/visible. – Tony Arnold Oct 28 '10 at 04:35