Loading an 8bpp (256 color) Bitmap Into an ImageList As a 32bpp Bitmap

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: MFC, Win32

One thing that always bugged me about ImageLists is that when you load a 256 color bitmap into them, they use the half-tone pallette, which likely could screw up your bitmap’s colors. Yet, Windows Explorer is magically able to display icons in it’s right pane list control (coming from an ImageList) in full color. So how does it do this?

WON’T WORK

(MFC)


BOOL CImageList::Create( UINT nBitmapID,
int cx,
int nGrow,
COLORREF crMask)
BOOL CImageList::Create( LPCTSTR lpszBitmapID,
int cx,
int nGrow,
COLORREF crMask)

(Win32)


HIMAGELIST ImageList_LoadBitmap( HINSTANCE hi,
LPCTSTR lpbmp,
int cx,
int cGrow,
COLORREF crMask)
HIMAGELIST ImageList_LoadImage( HINSTANCE hi,
LPCSTR lpbmp,
int cx,
int cGrow,
COLORREF crMask,
UINT uType,
UINT uFlags)

None of the above methods will do the trick. They will use the half-tone pallette, since they seem to detect the 8bpp nature of the bitmap and create an “ILC_COLOR8” ImageList, and ImageLists with the ILC_COLOR8 flag always use the half-tone pallette.

To get around this, you can create the ImageList in one step and then add the bitmap in a second step. In the below examples, I assume that you will be using a COLORREF mask for transparency – but nothing requires this to be the case; you could just as easily use a mask bitmap. The key to this technique is the two-step construction and loading of the ImageList.

WILL WORK

(MFC)


// Create the full-color image list
// cx, cy = your icon width & height
// You could also use ILC_COLOR24 rather than ILC_COLOR32

CImageList imgl;
imgl.Create(cx, cy, ILC_MASK | ILC_COLOR32, 0, 0);

CBitmap bmp;
// Load your imagelist bitmap (bmp) here, however
// you feel like (e.g. CBitmap::LoadBitmap)

COLORREF rgbTransparentColor;
// Set up your transparent color as appropriate

// Add the bitmap into the image list
imgl.Add(&bmp, rgbTransparentColor);

(Win32)


// Create the full-color image list
// cx, cy = your icon width & height
// You could also use ILC_COLOR24 rather than ILC_COLOR32

HIMAGELIST himgl = ImageList_Create(cx,
cy,
ILC_MASK | ILC_COLOR32,
0,
0);

HBITMAP hbmp;
// Load your imagelist bitmap (hbmp) here, however
// you feel like (e.g. LoadImage)

COLORREF rgbTransparentColor;
// Set up your transparent color as appropriate

// Add the bitmap into the image list
ImageList_AddMasked(himgl, hbmp, rgbTransparentColor);

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read