PRB: SelectObject() Fails After ImageList_GetImageInfo()

Last reviewed: September 29, 1995
Article ID: Q131279
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95 version 4.0
    

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 reference words: 4.00 unusable hDC Image Lists beta
KBCategory: kbui kbgraphic kbprb kbcode
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.