Microsoft KB Archive/299583

= How To Draw on a Memory Bitmap in GDI+ =

Article ID: 299583

Article Last Modified on 2/12/2007

-

APPLIES TO

 Microsoft Win32 Application Programming Interface, when used with:  Microsoft Windows XP Professional

 Microsoft Windows NT 4.0

 Microsoft Windows 2000 Standard Edition

 Microsoft Windows 98 Standard Edition</li></ul>

 Microsoft Windows Millennium Edition</li></ul> </li> Microsoft Windows XP Professional for Itanium-based systems</li></ul>

-

<div class="notice_section">

This article was previously published under Q299583

<div class="summary_section">

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.

<div class="moreinformation_section">

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.

<div class="references_section">