11.5 Deleting a Bitmap

A bitmap, like any resource, occupies memory while in use. After you have finished using a bitmap or before your application terminates, it is important that you delete the bitmaps you have created, in order to make that memory available to other applications. To delete a bitmap, first remove it from any device context in which it is currently selected. Then, delete it by using the DeleteObject function.

The following example deletes the bitmap identified by the hBitmap variable, after removing it as the currently selected bitmap in the memory device context identified by the hdcMemory variable:

SelectObject(hdcMemory, hOldBitmap);
DeleteObject(hBitmap);

The SelectObject function removes the bitmap from selection by replacing it with a previous bitmap identified by the hOldBitmap variable. The DeleteObject function deletes the bitmap. Thereafter, the bitmap handle in hBitmap is no longer valid and must not be used.