Click here to Skip to main content
11,570,759 members (55,757 online)
Click here to Skip to main content

Tagged as

Load a 256 color bitmap properly into an imagelist

, 2 Feb 2011 CPOL 11.2K 3
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
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.

You may also be interested in...

Comments and Discussions

 
GeneralReason for my vote of 5 Congratulation, I find what I need. Pin
manomano3-Feb-11 23:15
membermanomano3-Feb-11 23: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.150624.2 | Last Updated 2 Feb 2011
Article Copyright 2011 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid