Search code examples
c++bitmapmfcimagelist

Visual Studio C++ MFC: Displaying bitmap from imagelist


I have dialog with a picture control and I want to display an image of my imagelist. If I want to display a bitmap (no imagelist) by calling

CBitmap m_bmp;
m_bmp.LoadBitmap(IDB_BITMAP);
m_picture.SetBitmap(m_bmp); //m_picuture is the member of the picture control

it works, but my imagelist fails. Here's my code:

    m_bmparr.LoadBitmap(IDB_BITMAPARR); //bitmap is 144x48 (4 bit)
    m_imagelist.Create(48, 48, ILC_COLOR4, 0, 0); //3 * 48 = 144
    m_imagelist.Add(&m_bmparr, RGB(255, 0, 255));
    CBitmap* bitmap2;
    IMAGEINFO imgInfo;
    m_imagelist.GetImageInfo(1, &imgInfo); //Index 1 of imagelist
    bitmap2 = CBitmap::FromHandle(imgInfo.hbmImage);
    m_picture.SetBitmap(*bitmap2); //Show bitmap --> DOESN'T SHOW!! :(

Can anybody help me?


Solution

  • To create Image List from a large bitmap:

    m_imagelist.Create(IDB_BITMAPARR, 48, 0, RGB(255, 0, 255))
    

    Where icon width is 48, and bitmap width is 144 (or n * 48).

    Use ExtractIcon to create a new icon. Index starts at zero, therefore use ExtractIcon(0) for first icon.

    HICON hicon = m_imagelist.ExtractIcon(0);
    ICONINFOEX iinfo;
    iinfo.cbSize = sizeof(ICONINFOEX);
    GetIconInfoEx(hicon, &iinfo);
    
    m_picture.SetBitmap(iinfo.hbmColor);
    //destroy hicon when finished