You may sometime need to create a logical palette from a bitmap resource in
order to display the bitmap with the maximum number of available colors.
For example, on an 8 bit-per-pixel display, a logical palette is necessary
to draw a 256-color bitmap on a device context for that display. The
LoadBitmap function does not return or take a palette as one of its
parameters; thus, for example, there is no way to incorporate a palette
with a 256-color bitmap loaded with LoadBitmap. Therefore, an application
must load the resource as a device-independent bitmap (DIB), rather than a
device-dependent bitmap (DDB), in order to retrieve the bitmap's color
information. An application can use the FindResource, LoadResource, and
LockResource functions to do this.
A bitmap (.BMP file) is stored in an application's resources as a (DIB),
along with a color table if one exists. When a DIB is loaded from an
application's resources with the LoadBitmap function, a DDB is returned. This DDB is a bitmap compatible with the screen. Routines such as
CreateDIBitmap and SetDIBits that convert DIBs to DDBs take a handle to a
device context as their first parameter. This tells the routine what kind
of DDB to create. If this device context currently has a palette selected
into it, then CreateDIBitmap or SetDIBits can use this palette to create
the DDB. Without a palette, the routines are restricted to system colors
when matching the DIB's colors to the DDB's colors. For example, on an 8
bit-per-pixel display, the resulting DDB can have only up to 20 different
colors. With a logical palette, the resulting bitmap could have had up to
256 different colors.
If the bitmap is loaded as a DIB from the resource, then an application can
query the DIB's color table and create a logical palette for the DIB. Then,
it can call either CreateDIBitmap or SetDIBits, along with a device context
with that palette selected, to obtain a DDB compatible with that palette.
To load a bitmap from a resource as a DIB, you can use the FindResource function with the RT_BITMAP flag set and then use the LoadResource function to load it. You can lock the resource with the LockResource function.
The following code demonstrates how to use the above technique to load a
DIB from an application's resources, create a palette for it, and then
create a DDB out of it. The LoadResourceBitmap function below can be used in place of the LoadBitmap function. The only additional parameter needed is the address of a logical palette handle. The palette handle referenced will contain a handle to a logical palette after the function is called.
On Win32s, LoadResource does not return a true global memory handle. This
causes CreateDIBitmap to fail. To work around this problem, use GlobalAlloc
to create a global memory handle.
Here is what LoadResourceBitmap should be on Win32s:
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.