Click here to Skip to main content
Click here to Skip to main content

Tagged as

Load a 256 color bitmap properly into an imagelist

, 2 Feb 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)
 
BOOL CImageList::Create( UINT nBitmapID,
                           int cx,
                           int nGrow,
                           COLORREF crMask)
BOOL CImageList::Create( LPCTSTR lpszBitmapID,
                           int cx,
                           int nGrow,
                           COLORREF crMask)
 
(Win32 Version)
 
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)
 
// 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)
 
// 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)

Share

About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
C++ is not C with classes, JQuery is not Javascript, Google Search is not Learning, Design Patterns are not fashion, A code written in 2005 is not backdated just because it's 2015
Follow on   Twitter

Comments and Discussions

 
GeneralReason for my vote of 5 Congratulation, I find what I need. Pinmembermanomano4-Feb-11 0:15 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 2 Feb 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid