[futurebasic] Re: [FB] Replacement for QuickDraw window capture

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2010 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Sat, 17 Jul 2010 08:50:15 -0400
Brian wrote:

>Just started reading but "Creating an NSWIndow Object for the Carbon 
>Window" ...


The following will give you a full screenshot saved as a TIFF on your 
Desktop in Leopard.

CGWindowListCreateImage should allow you to pass a window ID and rect 
holding the window bounds for a snapshot of an individual window. 
However I have not been successful in implementing that.

There is a bug in Snow Leopard making this code work only in 32-bit. 
The REMed reference describes a simple work-around.

Please note: Change the path in the code to reflect your Desktop.


// http://www.mail-archive.com/cocoa-dev@.../msg43637.html

#import <ApplicationServices/ApplicationServices.h>

int SaveScreenshot() {

  CGImageRef windowImage = CGWindowListCreateImage( CGRectInfinite, 
kCGWindowListOptionOnScreenOnly, kCGNullWindowID, 
  if( CGImageGetWidth( windowImage ) <= 1)
      CGImageRelease( windowImage );
      return -1;

   NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] 
initWithCGImage: windowImage];
   NSImage *image = [[NSImage alloc] init];
   [image addRepresentation: bitmapRep];
   NSData *tiff = [image TIFFRepresentation];
// Change the following path to reflect your Desktop
   NSString *saveFile = [NSString 
   [tiff writeToFile:saveFile atomically:YES];
   [image release];
   [bitmapRep release];
   [tiff release];
   [saveFile release];

return 0;

toolbox fn SaveScreenshot() = int

window 1, "Test for Screenshot", (20, 20 )-( 300, 500 )

print "Sceenshot test"

print fn SaveScreenshot()