How to Use a DIB Stored as a Windows Resource

Last reviewed: November 2, 1995
Article ID: Q67883
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

Device-independent bitmaps (DIBs) are a very useful tool for displaying graphic information in a variety of device environments. With the appropriate device drivers, Windows can display a DIB with varying results on any video display or on a graphics printer.

This article discusses the differences between two methods that can be used to access a DIB from a resource.

MORE INFORMATION

Bitmaps retrieved from resources are very similar to those stored in .BMP files on disk. The header information is the same for each type of bitmap. However, depending upon the method used to retrieve the bitmap from the resource, the bitmap may be a device-independent bitmap (DIB) or a device-dependent bitmap (DDB).

When the LoadBitmap() function is used to obtain a bitmap from a resource, the bitmap is converted to a DDB. Typically, the DDB will be selected into a memory device context (DC) and blt'ed to the screen for display.

NOTE: If a 256-color bitmap with a palette is loaded from a resource, some colors will be lost. To display a bitmap with a palette correctly, the palette must be selected into the destination DC before the image is transferred to the DC. LoadBitmap() cannot return the palette associated with the bitmap; therefore, this information is lost. Instead, the colors in the bitmap are mapped to colors available in the default system palette, and a bitmap with the system default color depth is returned.

For example, if LoadBitmap() loads a 256-color image into an application running on a VGA display, the 256 colors used in the bitmap will be mapped to the 16 available colors, and a 4 bits-per-pixel bitmap will be returned. When the display is a 256-color 8514 unit, the same action will map the 256 bitmap colors into the 20 reserved system colors, and an 8 bits-per-pixel bitmap will be returned.

If, instead of calling LoadBitmap(), the application calls FindResource() (with RT_BITMAP type), LoadResource(), and LockResource(), a pointer to a packed DIB will be the result. A packed DIB is a BITMAPINFO structure followed by an array of bytes containing the bitmap bits.

NOTE: If the resource was originally stored as a DDB, the bitmap returned will be in the DDB format. In other words, no conversion is done.

The BITMAPINFO structure is a BITMAPINFOHEADER structure and an array of RGBQUADs that define the colors used in the DIB. The pointer to the packed DIB may be used in the same manner as a bitmap read from disk.

NOTE: The BITMAPFILEHEADER structure is NOT present in the packed DIB; however, it is present in a DIB read from disk.

REFERENCES

For sample code demonstrating how to use FindResource() with RT_BITMAP, LoadResource(), and LockResource(), please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q124947
   TITLE     : Retrieving Palette Information from a Bitmap Resource


Additional reference words: 3.00 3.10 3.50 4.00 95
KBCategory: kbgraphic
KBSubcategory: GdiBmp


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: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.