Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC
Tip/Trick

Load a 256 color bitmap properly into an imagelist

Rate me:
Please Sign up or sign in to vote.
4.80/5 (5 votes)
2 Feb 2011CPOL 24.6K   6   1
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)


Written By
Technical Lead Kotha Technologies
Bangladesh Bangladesh
If you are not in - you are out !
- Chapter 1

Comments and Discussions

 
GeneralReason for my vote of 5 Congratulation, I find what I need. Pin
manomano3-Feb-11 23:15
manomano3-Feb-11 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.