1. 程式人生 > >Win32從資源中載入PNG圖片,然後建立GDI+的Image物件

Win32從資源中載入PNG圖片,然後建立GDI+的Image物件

void LoadPNGFromStaticRes(HMODULE hModule, UINT nResId, Image** ppImg)
{
	HRSRC hRes = FindResource(hModule, MAKEINTRESOURCE(nResId), TEXT("PNG"));
	DWORD dwResSize = SizeofResource(hModule, hRes);

	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwResSize);
	CopyMemory(GlobalLock(hGlobal), LockResource(LoadResource(hModule, hRes)), dwResSize);

	IStream* pIStream;
	CreateStreamOnHGlobal(hGlobal, FALSE, &pIStream);
	*ppImg = Image::FromStream(pIStream);
	pIStream->Release();
	
	GlobalUnlock(hGlobal);
	GlobalFree(hGlobal);
}