Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking screenshot programmatically in mac only for application window

I have to add the ability to take screenshots to my OSX apps, but the Apple demo is only giving me the whole screen image, I just want to only my application window image.

My code is:

NSInteger displaysIndex = 0;
if(displays != nil)
{
    free(displays);
}


CGError             err = CGDisplayNoErr;
CGDisplayCount      dspCount = 0;

/* How many active displays do we have? */
err = CGGetActiveDisplayList(0, NULL, &dspCount);

/* If we are getting an error here then their won't be much to display. */
if(err != CGDisplayNoErr)
{
    return;
}
/* Allocate enough memory to hold all the display IDs we have. */
displays = calloc((size_t)dspCount, sizeof(CGDirectDisplayID));

// Get the list of active displays
err = CGGetActiveDisplayList(dspCount,
                             displays,
                             &dspCount);

/* Make a snapshot image of the current display. */
CGImageRef image = CGDisplayCreateImage(displays[displaysIndex]);

What should I change in code so I will get only my app's window?

like image 297
Bittu Avatar asked Mar 16 '13 06:03

Bittu


2 Answers

Simple.

NSImage *captureImage  = [[NSImage alloc] initWithData:[self.view dataWithPDFInsideRect:[self.view bounds]]];

please check and let me know. This is captured current active window.

like image 85
Solid Soft Avatar answered Sep 28 '22 16:09

Solid Soft


To actually take a screenshot of your window, including its frame and shadow, get your window's windowNumber and pass that to the CGWindowListCreateImage function.

like image 33
Peter Hosey Avatar answered Sep 28 '22 17:09

Peter Hosey