VC6.0 显示 BMP/JPG/GIF类型的图片

作者在 2008-07-22 12:54:36 发布以下内容
总结前人经验,现在共享给大家,希望能找到更好的方法。
建一个MFC的VC工程:
在资源中增加:
/////////////////////////////////////////////////////////////////////////////
//
// IMAGE
//
IDP_PIC IMAGE   DISCARDABLE     "res\\BHTLOG.jpg"
 
在MFC工程中加一个Picture Control(ID=IDC_PIC),关联 CStatic m_PIC。;
 
显示图片的函数:
BOOL ShowJpgFromRes(CWnd* pWnd,UINT nIDRes)
{
 IPicture *pPic = NULL;  
 IStream *pStm = NULL;   
 CFileStatus fstatus;  
 CDC* pDC = pWnd->GetDC(); 
 // find resource in resource file
 HINSTANCE hInst = AfxGetResourceHandle();
 HRSRC hRsrc = ::FindResource(hInst,MAKEINTRESOURCE(nIDRes),"IMAGE"); // type
 if (!hRsrc) return FALSE; 
 // load resource into memory
 DWORD cb = SizeofResource(hInst, hRsrc);
 BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
 if (!lpRsrc) return FALSE; 
 // create memory file and load it
 CMemFile file(lpRsrc, cb);
 FreeResource(hRsrc);
 
 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);   
 if (!hGlobal) return FALSE;
 LPVOID pvData = GlobalLock(hGlobal); 
 if (!pvData) return FALSE; 
 file.ReadHuge(pvData, cb);  
 GlobalUnlock(hGlobal); 
 
 CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);  
 
 if(!SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))  return FALSE;
 
 OLE_XSIZE_HIMETRIC hmWidth;  
 OLE_YSIZE_HIMETRIC hmHeight;
 
 pPic->get_Width(&hmWidth);  
 pPic->get_Height(&hmHeight); 
 
 CRect rec;
 pWnd->GetClientRect(&rec);     
 //double fX,fY;  
 //fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);  
 //fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);  
 //if(FAILED(pPic->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))  
 if(FAILED(pPic->Render(*pDC,rec.left,rec.top,rec.Width(),rec.Height(),0,hmHeight,hmWidth,-hmHeight,NULL)))
 {
  return FALSE;
 }
 pPic->Release();  
 return TRUE; 
}
调用方法:
void CTestDlg::OnTest()
{
 // TODO: Add your control notification handler code here
 ShowJpgFromRes(&m_PIC,IDP_PIC);
}
(编程)Work | 阅读 17759 次
文章评论,共0条
游客请输入验证码
浏览317262次