I try to get the screenshot from all monitors connected with my MAC to one picture. I know, how I can do this if every monitor's screenshot will saved to different pictures, but it is not what I want. I found function CGGetDisplaysWithRect, but my solution don't work, because output picture is empty. I expect, that problem with function CGDisplayCreateImageForRect (*displays, rect), because first parameter must be CGDirectDisplayID type, but not CGDirectDisplayID*. But I can't find function, which can create one picture with some CGDirectDisplayID objects.
Help me please!!!
#include <stdio.h>
#include <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
CGDisplayCount displayCount;
CGDirectDisplayID displays[32];
memset(&displays, 0, 32);
CGImageRef image[32];
CGRect rect = CGRectNull;
//grab the active displays
if (CGGetActiveDisplayList(32, displays, &displayCount) != kCGErrorSuccess)
{
printf("Error occured: %s
", strerror(errno));
}
//go through the list
for (int i = 0; i < displayCount; i++)
{
if (CGDisplayMirrorsDisplay(displays[i]) != kCGNullDirectDisplay)
{
continue;
}
//return the smallest rectangle wich contain the two source rectangles
rect = CGRectUnion(rect, CGDisplayBounds(displays[i]));
if (CGRectIsNull(rect))
{
printf("Error: %s", strerror(errno));
}
}
CGFloat whitePoint[3];
CGFloat blackPoint[3];
CGFloat gamma[3];
CGFloat matrix[9];
CGColorSpaceRef colorSpace = CGColorSpaceCreateCalibratedRGB (&whitePoint[3], &blackPoint[3], &gamma[3], &matrix[9] );
if(colorSpace == NULL)
{
printf("Error: %s", strerror(errno));
}
//CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
//Create bmp context for image
CGContextRef context = CGBitmapContextCreate(NULL, //data
CGRectGetWidth(rect), //width
CGRectGetHeight(rect), //height
8, //bitPerComponent, for RGB must be 8
0, //if data == NULL, it must be 0
colorSpace, //colorspace device independent
kCGBitmapByteOrderDefault ); //bitmap info
if(context == NULL)
{
printf("Error: %s", strerror(errno));
}
//Create a snapshot image
for (int i = 0; i < displayCount; i++)
{
image[i] = CGBitmapContextCreateImage(context);
if(image == NULL)
{
//printf("Error: %s", strerror(errno));
}
}
//Create destination to image
CFURLRef url = CFURLCreateWithString ( kCFAllocatorDefault, CFSTR("out.bmp"), NULL);
if(url == NULL)
{
printf("Error: %s", strerror(errno));
}
CFErrorRef *error = NULL;
CFURLRef urlToFile = CFURLCreateFilePathURL ( kCFAllocatorDefault, url, error );
if(urlToFile == NULL)
{
//printf("Error: %s", error);
}
CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL(urlToFile, kUTTypeBMP, displayCount, NULL);
if(imageDestination == NULL)
{
printf("Error: %s", strerror(errno));
}
//CGImageDestinationAddImage(imageDestination, image, NULL);
CGImageDestinationFinalize(imageDestination);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CFRelease(imageDestination);
return 0;
}
APDATE: I tried smth that me told below, but now I get error:
Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 3456 bytes/row.
See Question&Answers more detail:os