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
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 mails | Author | Date |
|---|---|---|
| Nir Soffer | Apr 23, 16:51 | |
| Dmitry Savenok | Apr 24, 13:11 | |
| Scott Thompson | Apr 24, 15:57 | |
| Shawn Erickson | Apr 26, 05:35 | |
| Nir Soffer | Apr 26, 15:59 | |
| Shawn Erickson | Apr 26, 16:17 | |
| Shawn Erickson | Apr 26, 19:17 | |
| Scott Thompson | Apr 26, 20:14 | |
| Uli Kusterer | Apr 27, 11:46 |






Cocoa mail archive

