加载不同格式类型图片封转函数(流中加载)

 定义各种图片变量

 Gdiplus::Image *m_bgImg;
 Gdiplus::Image *m_logoImg;
 Gdiplus::Image *m_titleImg;
 Gdiplus::Image *m_shutImg;
 Gdiplus::Image *m_tvBg;
 Gdiplus::Image *m_autoImg;
 Gdiplus::Image *m_powerON;
 Gdiplus::Image *m_powerONOver;

加载

int iResult = 0;

 //加载资源图片
 iResult += LoadImageFromResourceID( IDR_BG_JPG, RES_JPG, m_bgImg );

函数实现


//////////////////////////////////////////////////////////////////////////
//
// LoadImageFromResourceID 从资源中加载图片,然后从内存中创建Image对象
//
// 程序退出的时候需要手动删除pImg,释放资源
//
//////////////////////////////////////////////////////////////////////////

BOOL CMonitorSystemDlg::LoadImageFromResourceID( UINT nResID, UINT nResType, Gdiplus::Image* &pImg )
{
 HRSRC hRes = NULL;

 if ( nResType == RES_JPG )
 {
  hRes = FindResourceW( NULL, MAKEINTRESOURCEW( nResID ), L"JPG" );
 }
 if ( nResType == RES_PNG )
 {
  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 = Image::FromStream( pStream );

 pStream->Release();

 return TRUE;
}

释放资源句柄

#define ReleasePtr( p ) { if ( p ){ delete p; p = NULL; }}

 ReleasePtr( m_bgImg );

贴图方式显示

 CDC* pDC = pDlg->GetDC();

  if ( hMemDC == NULL )
  {
   hMemDC = CreateCompatibleDC( pDC->m_hDC );
   hBitmap = CreateCompatibleBitmap( pDC->m_hDC, pDlg->m_rtFullScreen.Width(), pDlg->m_rtFullScreen.Height());
   SelectObject( hMemDC, hBitmap );
  }

  Graphics graphics( hMemDC );

  graphics.DrawImage( pDlg->m_bgImg, 0, 0, pDlg->m_rtBg.left, pDlg->m_rtBg.top, pDlg->m_rtBg.Width(), pDlg->m_rtBg.Height(), UnitPixel );


  BitBlt( pDC->m_hDC, 0, 0, pDlg->m_rtFullScreen.Width(), pDlg->m_rtFullScreen.Height(), hMemDC, 0, 0, SRCCOPY );

  pDlg->ReleaseDC( pDC );

 

注:资源释放很重要

你可能感兴趣的:(加载不同格式类型图片封转函数(流中加载))