tags:

views:

88

answers:

2

I inserted bitmaps in CImageList in one Function and needed to change some of the images later in another function. But I am unable to extract the CBitmap. The code goes something like this:

CBitmap GetIndividualBitmap(CImageList oImgList, int nBmpNo) {

IMAGEINFO imgInfo;
imagelist.GetImageInfo(index,imgInfo);
CBitmap bmp;
bmp.FromHandle(imgInfo.hbmImage);
return bmp;

}

However all I get is a black screen. Could anyone please point out where I am going wrong?

A: 

A Google search on "CImageList Get Bitmap" produced http://www.codeguru.com/forum/archive/index.php/t-257564.html and http://www.codeproject.com/KB/graphics/getimagefromlist.aspx both of which seems like precisely what you are looking for?

Ruddy
i know. But I am trying to find out what is wrong with my code.
dev ray
According to the first link the HBITMAP returned is the entire original bitmap - you need to select the portion of it that is the image you want, which is in the imgInfo.rcImage; I can't tell you precisely why your current bitmap is all black when rendered - but i'll gander a guess is that its not screen dc compatible bitmap. Hence the code in both link's examples of how to copy just the section you need into a compatible bitmap.
Ruddy
+3  A: 

Ok there are a number of errors in your code

1)You are passing the Image list by object which means it will copy it across. Passing it by reference is a far better plan.
2) You are not passing a pointer to the IMAGEINFO struct into the GetImageInfo.
3) You misunderstand how "FromHandle" works. FromHandle is a static function that returns a pointer to a Bitmap. In your code you are calling the function and then ignoring the CBitmap* returned and returning a copy of your newly constructed object (ie it contains nothing) which results in your black screen.

Taking all those into account you should have code that looks like this:

CBitmap* GetIndividualBitmap(CImageList& oImgList, int nBmpNo) 
{
    IMAGEINFO imgInfo;
    oImgList.GetImageInfo( nBmpNo, &imgInfo );
    return CBitmap::FromHandle( imgInfo.hbmImage );
}
Goz
All very true. However, i believe you will still have to fetch the proper portion of the bitmap as indicated in my comments above.
Ruddy