Get pixel from the screen (screenshot) in Max OSX [duplicate]

馋奶兔 提交于 2019-12-23 06:58:39

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/17334786/get-pixel-from-the-screen-screenshot-in-max-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!