Click here to Skip to main content
15,867,307 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I have a pointer to a data block which happen to be binary data read from jpeg image file. I create IStream for it, and create bitmap from the stream in my function as following:

.....
IStream* pStream;
DWORD dwresult = CreateStreamOnHGlobal(NULL, TRUE, &pStream);
pStream->Write(pData, (ULONG)nSize, NULL);

// Create bitmap
Bitmap* pBitmap;
pBitmap = Bitmap::FromStream(pStream);

HBITMAP hBitmap = NULL;
pBitmap->GetHBITMAP(Color(0,0,0),&hBitmap);

CBitmap cbitmap;
cbitmap.Attach(hBitmap);

// imgLst is a CImageList 
imgList.Add(&cbitmap, RGB(0,0,0));
....



Question is that whether the image data is duplicate somewhere in this function?
(when create IStream, when create Bitmap object, when add to image list) I want to make sure there is no mem leak or premature release.

Thanks in advance

Quy
Posted

1 solution

Hi Quy,

The Gdiplus::Bitmap object by itself produces no new data, it is just a connector to the relevant codec and the Gdiplus::Image data (your IStream).

When you succesfully call pBitmap->GetHBITMAP(Color(0,0,0),&hBitmap); the Gdiplus::Image data are read (from your IStream) and a Windows bitmap object is created by Gdi32.dll; it's HBITMAP is returned.

At that moment you may close the IStream and delete the HGLOBAL.

When you call imgList.Add(&cbitmap, RGB(0,0,0)); the ImageList_Add function copies the bitmap to an internal data structure, and you must call ::DeleteObject() on the HBITMAP.
In your case this will happen when your cbitmap object gets out of scope.

cheers,
AR
 
Share this answer
 
Comments
quyps 9-May-11 5:36am    
Thanks, that's exactly what I want to know. 5 from me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900