通过资源ID加载图片

BOOL CTranceDlg::LoadImageFromResourceID( UINT nResID, Gdiplus::Image* &pImg )
{
 HRSRC hRes = FindResourceW( NULL, MAKEINTRESOURCEW( nResID ), L"PNG" );

 if ( NULL == hRes )
 {
  return FALSE;
 }

 DWORD dwSize = SizeofResource( NULL, hRes );

 HGLOBAL hGlobal = LoadResource( NULL, hRes );
 HGLOBAL hMem = GlobalAlloc( GMEM_FIXED | GMEM_MOVEABLE, dwSize );

 if ( NULL == hMem )
 {
  return FALSE;
 }

 BYTE* lpRes = ( BYTE* )LockResource( hGlobal );
 BYTE* lpMem = ( BYTE* )GlobalLock( hMem );

 memcpy( lpMem, lpRes, dwSize );

 IStream* pStream = NULL;

 CreateStreamOnHGlobal( lpMem, TRUE, &pStream );

 GlobalUnlock( hMem );

 pImg = Gdiplus::Image::FromStream( pStream );

 pStream->Release();

 return TRUE;
}

你可能感兴趣的:(通过资源ID加载图片)