Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Taking a screenshot of the screen?

Taking a screenshot of the screen?
Thread Tools
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Dec 26, 2002, 01:30 PM
 
OK, here's my objective.. I want to take a screenshot of a section of the screen and then save it as a JPEG. Here's what I have...

NSBitmapImageRep *grabbedImage = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 534, 386, 298)];

NSData *grabbedImageData = [grabbedImage representationUsingType:NSJPEGFileType properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.0f] forKey:@"NSImageCompressionFactor"]];

[[NSFileManager defaultManager] createFileAtPath: [[NSString stringWithString: @"~/Desktop/newlycreatefile.jpg"] stringByExpandingTildeInPath] contents: grabbedImageData attributes: [NSDictionary dictionaryWithObjectsAndKeys:@""]];

In this first line I am making a NSBitmapImageRep by giving it the coordinates of the screen I want to take a picture of. Then I am turning it into an NSData which I then use as the contents of the file I create in the third line. However this doesn't work... It compiles fine... I hit the button.. no warnings... On the desktop there is newlycreatedfile.jpg, however the Finder reports it as being 0 KB. Now dragging it on Chimera tells me it can't be displayed due to errors. And OmniWeb gives a big clue:

Cannot Load Address

Not a JPEG file: starts with 0xff 0xd9

Which leads me to believe that I am generating a TIFF file (or at least something that identifies itself as a TIFF file but has no content). And sure enough, changing the file extension to .tiff and dragging it onto OmniWeb doesn't show this error (though it doesn't show anything at all... sense it is zero bytes).

So what can I do? Am I approaching this wrong? How can I make it really a JPEG and have it actually have the data I put in there. Sorry for all the questions but I really need to get this working! Thanks
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Dec 26, 2002, 03:16 PM
 
You can't do that, I don't think. "FocusedViewRect" means just that - a rect in the view you are currently focused in (with lockFocus). You'll probably have to do something exciting with the screen base address pointer which you can get from CoreGraphics, or use some Carbon fun.
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Dec 31, 2002, 11:18 AM
 
Originally posted by Synotic:

Not a JPEG file: starts with 0xff 0xd9
I think it's interesting that a zero-byte file apparently contains at least the two bytes mentioned above. Technically, a zero-byte file is not in any format, tiff, gif, jpeg, doc or otherwise.

Kind of makes you wonder where OmniWeb is getting those bytes?
     
Mac Elite
Join Date: May 2002
Status: Offline
Reply With Quote
Dec 31, 2002, 07:44 PM
 
'twasn't 0 bytes; the finder said 0 KB, which is accurate to within 1024 bytes.
[vash:~] banana% killall killall
Terminated
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:59 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2