I am moving my code from GLUT to Cocoa (where I am a newbie), but I really like that GLUT on OSX allows you to copy a screenshot to the pasteboard just hitting cmd-c. The code below does that in a method belonging to a subclass of NSOpenGLView. It is largely a patchwork of ideas, which I found online.

Q: Could this have been achieved simpler? In particular, without resorting to a low level for loop and perhaps just cloning the first image rather than constructing a new bitmap image? Spawning a new process, I could probably do it with just a few lines, but that would probably be a rather heavy operation.

Hmm the code for snapping a screenshot seems to work and nobody has posted something better, so I am sticking with that. however, there were a few issues, so I am posting a new version. The main problem was the good old one-off issue: I was not writing to the top row of the image. That has been fixed. Also, I am now telling both Cocoa and OpenGL that the image should be densely packed in memory. Previously it was padded. Maybe that has slight performance gains, but this code is more clear, I think.

/Andreas

PS: Looking at the screenshot in Preview, I find that it is ever so slightly less saturated. That is probably something that preview does but curious all the same.

Thanks for your code. It works a treat. I did find two ways to get this to work without the for loops, presumably using vector transforms in the CPU or GPU. One way is to use an NSImage with a "setFlipped" property, but it appears this has been deprecated since OSX 10.6. The other option is to use Core Image. I show this below. Here the user can specify to either write to a PNG image or to the clipboard.