When you use GDI calls (such as TextOut) to write on the client area of your window, you're actually writing to a block of memory (the video display memory) that is organized much like a giant bitmap. The width and height of this bitmap are equal to the resolution of the video adapter. The manner in which multiple bits define color is also defined by the video adapter. Windows should also be able to pretend that a block of regular memory is video display memory. It should be able to write on this memory the same way it writes on the screen. We should then be able to use this block of memory as a bitmap.
That's exactly what a memory device context is. It helps us fill up and manipulate bitmaps in a Windows program. Here are the steps involved:
1.Create a memory device context using the CreateCompatibleDC call. Initially, the display surface of this memory device context contains one monochrome pixel. You can think of this device context as being 1 pixel high and 1 pixel wide, with two colors (black and white).
2.Create an uninitialized bitmap using CreateBitmap, CreateBitmapIndirect, or CreateCompatibleBitmap. When you create the bitmap, you specify the height and width and the color organization. However, the pixels of the bitmap need not actually represent anything yet. Save the handle to the bitmap.
3.Select the bitmap into the memory device context using SelectObject. Now the memory device context has a display surface that is the size of the bitmap with the same number of colors as defined by the bitmap.
4.Use GDI functions to draw on the memory device context the same way you use GDI functions to draw on a normal device context. Anything you draw within the display surface of the memory device context is actually drawn on the bitmap selected into the device context.
5.Delete the memory device context. You are left with a handle to a bitmap that contains a pixel representation of what you drew on the memory device context.