Memory Buffer into GDI+ Image(FromStream)

M204
M204 used Ask the Experts™
on
I have the following code that loads a bitmap file from a zip file (using Zip Utils) and copies the memory buffer into a HGblobal, so that it can be loaded into a GDI+ Image object from a Stream.
The code compiles fine - however the bitmap is corrupt. Am I using the correct methods here?

Appreciate any thoughts or suggestions.
void CASI::LoadResources(std::wstring strFolder)
{
	std::wstring strPath; 
	HGLOBAL hBuffer;
 
	strPath = strFolder + L"ASI.zip";
	HZIP hz = OpenZip(strPath.c_str(),0);
	ZIPENTRY ze; int i; FindZipItem(hz,L"background.bmp",true,&i,&ze);
	char *ibuf = new char[ze.unc_size];
	hBuffer = ::GlobalAlloc(GMEM_MOVEABLE,ze.unc_size);
	UnzipItem(hz, i, ibuf, ze.unc_size);
 
	if(hBuffer)
	{
		void* pBuffer = ::GlobalLock(hBuffer);
		if(pBuffer)
		{
			CopyMemory(pBuffer, ibuf, ze.unc_size);
			IStream* pStream = NULL;
 
			if(::CreateStreamOnHGlobal(hBuffer,FALSE,&pStream) == S_OK)
			{
				m_BackImage = Gdiplus::Image::FromStream(pStream,false);
				pStream->Release();
				if(m_BackImage)
				{
					if(m_BackImage->GetLastStatus() == Ok)
					{
						::GlobalUnlock(hBuffer);
						::GlobalFree(hBuffer);
					}
				}
			}
			::GlobalUnlock(hBuffer);
		}
		::GlobalFree(hBuffer);          
	}
	delete[] ibuf;
	CloseZip(hz); 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I don't think that you cannot free the memory block until you delete the Image object.  I believe that you can specify that the memory block be free'd on the last release of the storage, however.

Commented:
Make that 'I don't think you CAN free the memory block...'
jkr
Top Expert 2012

Commented:
Your code seems to be pretty identical to http://www.codeproject.com/KB/GDI-plus/cgdiplusbitmap.aspx - are you sure the bitmap format in the memory buffer is correct? If that is a plain .bmp file, this is quite unlikely to work.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Commented:
It is not identical.  On success, the code to which you refer returns without deleting the HGLOBAL memory block.  He is deleting the block before he returns.  I do not believe that you can do that.  Gdiplus does not keep a copy of the image in memory, and therefore needs the stream for the life of the object.

Author

Commented:
Many thanks for your help - this works great now.
Andrew

Commented:
By the way, Gdiplus can read .jpg and .png files as well.  You might want to simply convert your .bmp file to one of those formats before you put it in the resource.  This would let you avoid the need to zip the file, and would reduce the size of the memory block you would need to allocate.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial