PRB: SelectObject() Fails After ImageList_GetImageInfo()

ID: Q131279

The information in this article applies to:

SYMPTOMS

When you try to select the hBitmap returned by ImageList_GetImageInfo() into a device context, the call to SelectObject() fails and returns NULL.

CAUSE

Under the debug version of Windows 95, attempting to select the hBitmap returned by ImageList_GetImageInfo() into a DC causes the GDI to output the message "Bitmap already selected."

Image lists maintain memory DCs with the image and mask bitmaps already selected. This is done to prevent applications from modifying the images contained in the image list that are currently being used by the system. Because a bitmap cannot be selected into more than one DC at a time, applications that call SelectObject() on the same bitmap fail.

RESOLUTION

An application can work around this in Windows 95 by calling CopyImage() on the hBitmap, as demonstrated in the following sample code. This API is new for Windows 95. Remember to delete the hBitmap copy when using this function.

Sample Code

   HDC        hDC;
   HBITMAP    hBitmap, hOldBitmap;
   IMAGEINFO  imageInfo;

   ImageList_GetImageInfo (hImgList, iWhichImage, &imageInfo);
   hBitmap = CopyImage (imageInfo.hbmImage,
                        IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);

   hOldBitmap = SelectObject(hDC, hBitmap);
     :
     :

   // Delete hBitmap when you finish using it.
   DeleteObject (SelectObject (hDC, hOldBitmap));

STATUS

This behavior is by design.

Additional query words: unusable hDC Image Lists beta

Keywords          : kbCtrl kbImgList kbGrpUser kbWinOS95 
Issue type        : kbprb

Last Reviewed: January 2, 1999