Advertisement
jckuri

This captures the screen of Macs and saves it to a file.

Jul 5th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. // g++ -o SaveScreen SaveScreen.cpp -framework Cocoa
  2.  
  3. #include </System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h>
  4. #include <objc/objc-runtime.h>
  5.  
  6. CGImageRef captureScreenImage(int x,int y,int width,int height) {
  7.  CGRect captureRect;
  8.  captureRect.origin.x = x;
  9.  captureRect.origin.y = y;
  10.  captureRect.size.width = width;
  11.  captureRect.size.height = height;
  12.  CGImageRef img = CGWindowListCreateImage(captureRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
  13.  return img;
  14. }
  15.  
  16. objc_object* charsToNSString(char *chars) {
  17.  objc_object *nsStringAlloc = objc_msgSend(objc_getClass("NSString"),sel_getUid("alloc"));
  18.  return objc_msgSend(nsStringAlloc,sel_getUid("initWithUTF8String:"),chars);
  19. }
  20.  
  21. void saveImage(CGImageRef img,char* filename) {
  22.  objc_object *ciImageObject = objc_msgSend(objc_getClass("CIImage"),sel_getUid("imageWithCGImage:"),img);
  23.  objc_object *imageRepObject = objc_msgSend(objc_getClass("NSBitmapImageRep"),sel_getUid("alloc"));
  24.  objc_object *bitmapRep = objc_msgSend(imageRepObject,sel_getUid("initWithCIImage:"),ciImageObject);
  25.  objc_object *properties = nil;
  26.  objc_object *jpegType = nil;
  27.  objc_object *blob = objc_msgSend(bitmapRep,sel_getUid("representationUsingType:properties:"),jpegType,properties);
  28.  objc_msgSend(blob,sel_getUid("writeToFile:atomically:"),charsToNSString(filename),NO);
  29.  printf("%s was written successfully.\n",filename);
  30. }
  31.  
  32. int main() {
  33.  saveImage(captureScreenImage(0,0,800,600),"desktop.png");
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement