How To Draw on a Memory Bitmap in GDI+ (299583)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows XP
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows 98
    • the operating system: Microsoft Windows Millennium Edition
  • the operating system: Microsoft Windows XP 64-Bit Edition

This article was previously published under Q299583

SUMMARY

You may sometimes want to obtain a bitmap or image that contains the image that was drawn by a graphics object.

One of the overloaded constructors for the Bitmap class takes a graphics object as a parameter. However, this constructor does not use the image drawn by the graphics object to initialize the bitmap bits. It simply creates a bitmap with properties similar to the graphics object, such as dots per inch.

MORE INFORMATION

Because the Bitmap constructor does not initialize the image bits by using the image from the graphics object, code like the following will NOT result in a bitmap that contains the image that was drawn by the Graphics object:
Graphics g( hWnd );
// Draw on g
Bitmap b( 100, 100, &g ); // Will not get image from g
				

To use a graphics object to draw on a bitmap, code like the following can be used instead:
Bitmap b(100,100);
Graphics *g = Graphics::FromImage(&b);
// Draw on g
				

To capture the preexisting image from a window, a Windows Graphics Device Interface (GDI) function such as BitBlt() or StretchBlt() would have to be used to copy the image from the screen to a memory bitmap. This memory bitmap could then be used in the overloaded Bitmap constructor, which takes an HBITMAP as a parameter.

REFERENCES

For additional information on capturing the screen using GDI, click the article number below to view the article in the Microsoft Knowledge Base:

186736 How To Capture and Print an Entire Window

"Capturing an Image" in the GDI section of the Platform SDK Documentation:

Modification Type:MinorLast Reviewed:4/4/2006
Keywords:kbDSWGDI2003Swept kbhowto KB299583