This is Part I of a series on creating a splash screen application in native
code. For more information, see the
Introduction and the
Code License.
Part I: Creating a HBITMAP
In order to display an image on-screen, we need to have it available as a
HBITMAP. The Windows Imaging Component allows us to decode a PNG image into a 32 bits-per-
pixel bitmap (with an alpha channel) and extract its pixels into a DIB.
In this code, I’ll assume the image is embedded in the resources of the splash
screen EXE (using a statement similar to the following in the .RC file):
IDI_SPLASHIMAGE PNG splash.png
The first step is to create an IStream on the resource data. This involves loading the
resource, copying its data into a memory buffer, then creating a stream on
that buffer. (Note that the following code has been changed for pedagogical
purposes; production code could be improved by using smart pointers for the
COM interfaces and Windows handles, and by using exceptions to handle error
conditions.) COM should have been initialised (by calling
CoInitialize or
CoInitializeEx) before calling this method.
Now that we have an IStream pointer to the data of the image, we can use WIC
to load that image. An important step in this process is to use
WICConvertBitmapSource to ensure that the image is in a 32bpp format
suitable for direct conversion into a DIB. This method assumes that the input
image is in the PNG format; for a splash screen, this is an excellent choice
because it allows an alpha channel as well as lossless compression of the
source image. (To make the splash screen image as small as possible, I highly
recommend the PNGOUT compression
utility.)
Next is to use CreateDIBSection to allocate a DIB that can be written to
directly. By setting up a BITMAPINFO structure with the right values, the DIB will
be of the same format as the 32bpp BGRA image loaded by WIC, and the pixels
can be copied directly from the WIC bitmap to the DIB.
// Creates a
Finally, these three functions can be put together to load the PNG image from
the EXE’s resources and convert it to a HBITMAP:
The next installment will show how to create a layered window that can display
this image.