troubleshooting Question

Memory Buffer into GDI+ Image(FromStream)

Avatar of M204
M204 asked on
C++
6 Comments1 Solution10107 ViewsLast Modified:
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); 
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros