How to make screenshot programmaticaly ?

How to make a screenshot from my own program code without using an external tool ?
There are several solutions:
the "old" way:
using the Device Manager routines, will give access to the screen thru a
CGrafPtr, you can then use this pointer with CopyBits.

the CoreGraphics way:
CGDisplayBaseAddress, it returns the base address of the buffer holding the screen bits, and CGDisplayPixelsWide, CGDisplayPixelsHigh, CGDisplayBitsPerPixel, CGDisplayBitsPerSample, CGDisplaySamplesPerPixel, CGDisplayBytesPerRow give you the required information to set up your own buffer to copy the screen bits into. the command line way:
usage: screencapture [-icmwsWx] [file]
-i capture screen interactively, by selection or window
control key - causes screen shot to go to clipboard
space key - toggle between mouse selection and
window selection modes
escape key - cancels interactive screen shot
-c force screen capture to go to the clipboard
-m only capture the main monitor, undefined if -i is set
-w only allow window selection mode
-s only allow mouse selection mode
-W start interaction in window selection mode
-x do not play sounds
-S in window capture mode, capture the screen not the window
file where to save the screen capture
that you may invoke from popen or NSTask.