Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // g++ -o SaveScreen SaveScreen.cpp -framework Cocoa
- #include </System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h>
- #include <objc/objc-runtime.h>
- CGImageRef captureScreenImage(int x,int y,int width,int height) {
- CGRect captureRect;
- captureRect.origin.x = x;
- captureRect.origin.y = y;
- captureRect.size.width = width;
- captureRect.size.height = height;
- CGImageRef img = CGWindowListCreateImage(captureRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
- return img;
- }
- objc_object* charsToNSString(char *chars) {
- objc_object *nsStringAlloc = objc_msgSend(objc_getClass("NSString"),sel_getUid("alloc"));
- return objc_msgSend(nsStringAlloc,sel_getUid("initWithUTF8String:"),chars);
- }
- void saveImage(CGImageRef img,char* filename) {
- objc_object *ciImageObject = objc_msgSend(objc_getClass("CIImage"),sel_getUid("imageWithCGImage:"),img);
- objc_object *imageRepObject = objc_msgSend(objc_getClass("NSBitmapImageRep"),sel_getUid("alloc"));
- objc_object *bitmapRep = objc_msgSend(imageRepObject,sel_getUid("initWithCIImage:"),ciImageObject);
- objc_object *properties = nil;
- objc_object *jpegType = nil;
- objc_object *blob = objc_msgSend(bitmapRep,sel_getUid("representationUsingType:properties:"),jpegType,properties);
- objc_msgSend(blob,sel_getUid("writeToFile:atomically:"),charsToNSString(filename),NO);
- printf("%s was written successfully.\n",filename);
- }
- int main() {
- saveImage(captureScreenImage(0,0,800,600),"desktop.png");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement