问题描述
- 在MFC的CDialog类中可以显示JPG图片 在CFromview类中用同样的方法显示不了
-
HGLOBAL hMem;
IStream *m_pStream;
IPicture *m_pPicture;
OLE_XSIZE_HIMETRIC m_JPGWidth;
OLE_XSIZE_HIMETRIC m_JPGHeight;CRect rect(100,120,600,460); CString path; CFile file; CFileDialog dlg(TRUE,_T("jpg"),_T("*.jpg"), OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif|bmp文件(*.bmp)|*.bmp|所有文件(*.*)|*.*|"),NULL); if(dlg.DoModal()==IDOK) { path=dlg.GetPathName(); } file.Open(path,CFile::modeReadWrite); DWORD len=file.GetLength(); hMem= GlobalAlloc(GMEM_MOVEABLE,len); LPVOID pData=NULL; pData=GlobalLock(hMem); file.Read(pData,len); file.Close(); GlobalUnlock(hMem); CreateStreamOnHGlobal(hMem,TRUE,&m_pStream); OleLoadPicture(m_pStream,len,TRUE,IID_IPicture,(LPVOID*)&m_pPicture); m_pPicture->get_Height(&m_JPGHeight); m_pPicture->get_Width(&m_JPGWidth); CDC *pDC=NULL; pDC=this->GetDC(); m_pPicture->Render(pDC->m_hDC,0,0,(int)rect.Width(),(int)rect.Height(),0,m_JPGHeight,m_JPGWidth,-m_JPGHeight,NULL);
解决方案
CFromview会引发重绘,打开文件得到文件名存在一个变量,在OnDraw里绘图。
时间: 2024-12-09 02:59:48