Question : Extract a CBitmap from a CImageList

I've used the resource editor to specify a toolbar-like bitmap (that is, it has about 30 images all concatenated together).

I then use the CImageList.Create() function to load that resource and turn it into an image list for use with tree and list controls, etc:

Create(UINT nBitmapID, int cx, int nGrow, COLORREF crMask);

Problem is, I can't see a way to get an individual CBitmap out of the image list for a specific image; that is,

CBitmap bmpToExtract;
RetrieveBitmap(CImageList *myList, UINT iImageNumber, &bmpToExtract);

The GetImageInfo() function apparently retrieves the entire bitmap, not just a bitmap for the image I want to extract.

I sure hope there's any easy answer I'm blindly missing....

Thanks,

--JWimbish

Answer : Extract a CBitmap from a CImageList

HBITMAP CImageListEx::GetImageBitmap(int i)
{
IMAGEINFO imInfo;

// get screen DC
CDC *pDC = CDC::FromHandle(::GetDC(NULL));

// use GetImageInfo to get image size, since all images have the // same size use 0 index
BOOL res  = GetImageInfo(0, &imInfo);

if( res == NULL )
      return NULL;
      
CSize imSize = CSize(imInfo.rcImage.right - imInfo.rcImage.left,
            imInfo.rcImage.bottom - imInfo.rcImage.top );

// create compatible DC, bitmap, selecting bitmap into memory DC
// and force image list to draw desired image onto memory DC
CDC memDC;
memDC.CreateCompatibleDC(pDC);
HBITMAP hBm = ::CreateCompatibleBitmap(pDC->GetSafeHdc(), imSize.cx, imSize.cy);
HBITMAP hOldBm = (HBITMAP)::SelectObject(memDC.GetSafeHdc(), hBm);
Draw(&memDC, i, CPoint(0, 0), ILD_NORMAL);
::SelectObject(memDC.GetSafeHdc(), hOldBm);

// return bitmap      
return hBm;

}
Random Solutions  
 
programming4us programming4us