加载图片资源的方法

 
BOOL CImageManager::LoadResImage(UINT nResID,
								 LPCTSTR lpType,
								 Bitmap *&lpImage, 
								 HINSTANCE hInstance)
{
	lpImage  = NULL;

	hInstance = (NULL == hInstance) ? ::AfxGetResourceHandle() : hInstance;
	// If bmp,use system load.
	if (RT_BITMAP == lpType)
	{
		HBITMAP hBmp = ::LoadBitmap(hInstance, MAKEINTRESOURCE(nResID));
		lpImage = Bitmap::FromHBITMAP(hBmp, 0);
		::DeleteObject(hBmp);

		if (!lpImage)
		{
			TRACE("lpImage is NULL\n");
			return FALSE;
		}
		else
		{
			if (Gdiplus::Ok != lpImage->GetLastStatus())
			{
				TRACE("lpImage is error\n");
				return FALSE;
			}
			else
			{
				return TRUE;
			}
		}
	}

	// User-Defined

	HRSRC hRsrc = ::FindResource(hInstance, MAKEINTRESOURCE(nResID), lpType);
	if (!hRsrc)
	{
		TRACE("hRscs is NULL\n");
		return FALSE;
	}

	// Load resource into memory ---------------------------------
	DWORD len = ::SizeofResource(hInstance, hRsrc);

	BYTE *lpRsrc = (BYTE*)::LoadResource(hInstance, hRsrc);
	if (!lpRsrc)
	{
		TRACE("lpRsrc is NULL\n");
		return FALSE;
	}

	// Allocate global memory on which to create stream ----------
	HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, len);

	BYTE *pMem = (BYTE*)::GlobalLock(hMem);
	memcpy(pMem, lpRsrc, len);

	IStream *pStream = NULL;
	// You can query MSDN,why I use TRUE, -- hgy notes.
	HRESULT ht = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);
	if (S_OK != ht)
	{
		TRACE("ht is error\n");
		return FALSE;
	}

	// Load from stream -------------------------------------------
	lpImage = Gdiplus::Bitmap::FromStream(pStream);

	// free/release stuff -----------------------------------------
	::GlobalUnlock(hMem);
	pStream->Release();
	::FreeResource(lpRsrc);

	if (!lpImage)
	{
		TRACE("lpImage is NULL\n");
		return FALSE;
	}
	else
	{
		if (Gdiplus::Ok != lpImage->GetLastStatus())
		{
			TRACE("lpImage is error\n");
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
}
自己写的,经常用到,记录下,总不能每次都重写吧。
原文地址:https://www.cnblogs.com/hgy413/p/3693566.html