Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Load a 256 color bitmap properly into an imagelist

4.80/5 (5 votes)
2 Feb 2011CPOL 25.1K  
This tip shows the technique of loading a 256 color bitmap into an image list
One thing that always bugged me about ImageLists is that when you load a 256 color bitmap into them, they use the half-tone palette, which likely could screw up your bitmap's colors. Yet, Windows Explorer is magically able to display icons in its right pane list control (coming from an ImageList) in full color. So how does it do this?

The following methods will not work:

(MFC Version)

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


(Win32 Version)

C++
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)


What will work is this:

(MFC Version)

C++
// 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 Version)
C++
// 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);


Copied From: http://www.ucancode.net/Visual_C_MFC_Example/Add-bitmap-to-CImageList-VC-Example.htm[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)