1

I've been trying to get a screenshot from a C++ program in MacOSX 10.8.4 and it's been impossible. I can't even get a single pixel. Is it even possible?

Can somebody help me? Or at least give me some clue or link.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • It's certainly possible - for instance, see the ["Son of Grab" sample code](https://developer.apple.com/library/mac/#samplecode/SonOfGrab/Introduction/Intro.html). If you tell us what you have tried, we can give you more specific help. – Kurt Revis Jun 27 '13 at 05:02

2 Answers2

4
void captureScreen(){
    CGImageRef image_ref = CGDisplayCreateImage(CGMainDisplayID()); 
    CGDataProviderRef provider = CGImageGetDataProvider(image_ref);
    CFDataRef dataref = CGDataProviderCopyData(provider);
    size_t width, height;    width = CGImageGetWidth(image_ref);
    height = CGImageGetHeight(image_ref); 
    size_t bpp = CGImageGetBitsPerPixel(image_ref) / 8;
    uint8 *pixels = malloc(width * height * bpp);
    memcpy(pixels, CFDataGetBytePtr(dataref), width * height * bpp);
    CFRelease(dataref); 
   CGImageRelease(image_ref); 
   FILE *stream = fopen("/Users/robert/Desktop/screencap.raw", "w+");
   fwrite(pixels, bpp, width * height, stream);
   fclose(stream); 
   free(pixels);
}

reference https://gist.github.com/robert-wallis/5063309

  • Thanks!!! That's what I wanted. But when I open the image with Photoshop 2 RGB channel are permuted. Any clue? –  Jun 27 '13 at 18:22
-3

If all you want is to grab an image of a window, try pressing the key sequence+shift+3 will take a snapshot of the entire screen, and place the resulting image on your desktop.

Replacing 3 with 4 will allow you to interactively select a region of the screen.

Finally, pressing +shift+4, then space will allow you to select a window to capture.

This page nicely describes a lot of methods for capturing content on the screen.

radical7
  • 8,957
  • 3
  • 24
  • 33