Skip navigation.
 
mlScreenshot code for Tiger
FROM : Nir Soffer
DATE : Sun Apr 23 16:51:24 2006

I searched for screenshot example code, and found few solutions, and 
its not clear which is the way to go.

First I found this code:

   NSView *view = [window contentView];
   [view lockFocus];
   NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] 
initWithFocusedViewRect:[view bounds]];
   [view unlockFocus];

But various comments suggest it does not work on Tiger any more. Anyone 
knows why?

Then there is Apple glGrab example code
http://developer.apple.com/samplecode/Sample_Code/Archive/Graphics/
glGrab.htm. The file was removed from Apple site - anyone knows why?

Then there is this code, that appear to be copied from glGrab.c:
http://www.cocoabuilder.com/archive/message/cocoa/2005/8/13/144256.

Then in http://www.cocoadev.com/index.pl?ScreenShotCode, there is code 
base on glGrab by http://www.cocoadev.com/index.pl?MikeAsh.

Then there is this code using Carbon in 
http://www.cocoadev.com/index.pl?ScreenShotCode, by 
http://www.cocoadev.com/index.pl?RyanBates:

+ (NSImage *)imageWithScreenShotInRect:(NSRect)cocoaRect
{
        PicHandle picHandle;
        GDHandle mainDevice;
        Rect rect;
        NSImage *image;
        NSImageRep *imageRep;

        // Convert NSRect to Rect
        SetRect(&rect, NSMinX(cocoaRect), NSMinY(cocoaRect), 
NSMaxX(cocoaRect), NSMaxY(cocoaRect));

        // Get the main screen. I may want to add support for multiple 
screens later
        mainDevice = GetMainDevice();

        // Capture the screen into the PicHandle.
        picHandle = OpenPicture(&rect);
        CopyBits((BitMap *)*(**mainDevice).gdPMap, (BitMap 
*)*(**mainDevice).gdPMap,
                                &rect, &rect, srcCopy, 0l);
        ClosePicture();

        // Convert the PicHandle into an NSImage
        // First lock the PicHandle so it doesn't move in memory while 
we copy
        HLock((Handle)picHandle);
        imageRep = [NSPICTImageRep imageRepWithData:[NSData 
dataWithBytes:(*picHandle)
                                         
length:GetHandleSize((Handle)picHandle)]];
        HUnlock((Handle)picHandle);

        // We can release the PicHandle now that we're done with it
        KillPicture(picHandle);

        // Create an image with the representation
        image = [[[NSImage alloc] initWithSize:[imageRep size]] 
autorelease];
        [image addRepresentation:imageRep];

        return image;
}

Finally, there is this code from sticksoftwate: 
http://www.sticksoftware.com/developer/Screensnap.m.txt

Maybe someone can clear the mess and explain which is the recommended 
and tested way to have a screenshot on Tiger?


Best Regards,

Nir Soffer

Related mailsAuthorDate
mlScreenshot code for Tiger Nir Soffer Apr 23, 16:51
mlRe: Screenshot code for Tiger Dmitry Savenok Apr 24, 13:11
mlRe: Screenshot code for Tiger Scott Thompson Apr 24, 15:57
mlRe: Screenshot code for Tiger Shawn Erickson Apr 26, 05:35
mlRe: Screenshot code for Tiger Nir Soffer Apr 26, 15:59
mlRe: Screenshot code for Tiger Shawn Erickson Apr 26, 16:17
mlRe: Screenshot code for Tiger Shawn Erickson Apr 26, 19:17
mlRe: Screenshot code for Tiger Scott Thompson Apr 26, 20:14
mlRe: Screenshot code for Tiger Uli Kusterer Apr 27, 11:46