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

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);


Comments

  • image

    Posted by Legacy on 02/02/2004 12:00am

    Originally posted by: priya

    how to take image(any jpeg,bmp,etc) as input in c language program

    Reply
  • Which Lib File must need?

    Posted by Legacy on 09/23/2003 12:00am

    Originally posted by: Sukhyun

    In My Project, linker unresolved external symbol __imp__ImageList_LoadImageA@28.
    Which Library I must import in this project?

    Reply
  • How to read an image from *.pdf, *.jpg, *.bmp files

    Posted by Legacy on 06/06/2003 12:00am

    Originally posted by: Muhammad Faisal ali

    How to read an image from *.pdf, *.jpg, *.bmp files. I wanna take some guidence about this regards please help me as soon as possible voluntarily. I never forget your help.

    Reply
  • control checkboxes

    Posted by Legacy on 05/20/2003 12:00am

    Originally posted by: Mihaela

    how can i control the checkboxes assigned to an item from an ImageList: for modify their size, position and apereance

    Reply
  • Still no happy

    Posted by Legacy on 03/26/2003 12:00am

    Originally posted by: joghurt

    This still doesn't work for me. It still screws up colors and won't make the propers mask. (Win2000 and XP)

    I did a simple test: a bitmap with the standard 16 Windows colors. Then added it to the image list with various colours as the transparent colour. For RGB(0,0,0) the colours were completely broken. If the transparent colour had been RGB (255,0,0), RGB (0,255,0), RGB (0,0,255) or RGB (0,128,128), ImageList_AddMasked was simply unable to generate the proper mask. However, for values RGB(255,255,0), RGB(255,0,255), RGB(0,255,255), RGB(128,128,128), RGB(192,192,192) and RGB(255,255,255) it was as it should be.

    By the way, according to Microsoft,
    "Bitmaps with color depth greater than 8bpp are not supported" for ImageList_AddMasked.

    Any comments/ideas?

    Reply
  • how to add the image to the resource ?

    Posted by Legacy on 01/06/2003 12:00am

    Originally posted by: shangxiaolei

    Thank you guy, but I don't know how to get the nBitmap's ID.Could somebody tell me.I will be very thankful.

    Reply
  • How to loading an 8bpp (256 color) Bitmap File Into an ImageList As a 32bpp Bitmap

    Posted by Legacy on 12/28/2002 12:00am

    Originally posted by: marfos

    .help i need more color in my life

    Reply
  • Aligning Image on a Button

    Posted by Legacy on 12/23/2002 12:00am

    Originally posted by: Vipin Singh


    How can we align a image on a button. We are creating imagelist and then adding images to it using ImageList_AddMasked and then sending the message to add the buttons using SendMessage. But the images are rightaligned on the button. Does any one has idea how can we left align or center align a image on the button. This is urgent. Pls. reply asap and pls. copy replies to my ID also.

    Thanks,
    Vipin.

    Reply
  • Excellent work, but why?

    Posted by Legacy on 10/19/2002 12:00am

    Originally posted by: Michael

    Why we should do this in 2 step?

    Reply
  • thanks

    Posted by Legacy on 10/15/2002 12:00am

    Originally posted by: gengyf

    it help me in time! thanks very much!

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • In this webinar, IDC featured speaker Steve Conway, Vice President of High Performance Computing, will present an update on the global x86 HPC cluster market. The presentation will include IDC's five-year forecast for the medium- to large-scale technical computing and data analysis emerging markets by systems, processors and application middleware. Cray's featured speaker, John Lee, Vice President of Cray Cluster Advanced Technology Systems, will present the new Cray® CS400™ cluster series based on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds