Deleting Bitmaps

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 parameter, after removing it as the currently selected bitmap in the memory device context identified by the hMemoryDC parameter:

SelectObject(hMemoryDC, hOldBitmap);

DeleteObject(hBitmap);

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