MFC显示JPG图片
2016-05-29 01:03
253 查看
参考文章:http://blog.csdn.net/crearo/article/details/1328974
-----------------------------------------------------------------------------------------------
效果截图:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
主要代码如下:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
例子工程(VS2010)下载地址:http://download.csdn.net/detail/friendan/7709595
-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
效果截图:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
主要代码如下:
//************************************ // 方法说明: 显示JPG和GIF、BMP图片 // 参数说明: CDC * pDC 设备环境对象 // 参数说明: CString strPath 要显示的图片路径 // 参数说明: int x 要显示的X位置 // 参数说明: int y 要显示的Y位置 // 返回值: BOOL 成功返回TRUE,否则返回FALSE //************************************ BOOL CShowJpgDlg::ShowJpgGif(CDC* pDC,CString strPath, int x, int y) { CFileStatus fstatus; CFile file; ULONGLONG cb; // 打开文件并检测文件的有效性 if (!file.Open(strPath,CFile::modeRead)) { return FALSE; } if (!file.GetStatus(strPath,fstatus)) { return FALSE; } if ((cb =fstatus.m_size)<=0) { return FALSE; } // 根据文件大小分配内存空间,记得释放内存 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, (unsigned int)cb); if (hGlobal== NULL) { return FALSE; } // 锁定刚才分配的内存空间 LPVOID pvData = NULL; pvData = GlobalLock(hGlobal); if (pvData == NULL) { GlobalFree(hGlobal); // 记得释放内存 return FALSE; } // 将文件放到流中 IStream *pStm; file.Read(pvData,(unsigned int)cb); GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); // 从流中加载图片 // 显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画, // 要显示动画GIF请使用ACTIVE控件。 IPicture *pPic; if(OleLoadPicture(pStm,(LONG)fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)!=S_OK) { GlobalFree(hGlobal); // 记得释放内存 return FALSE; } //获取图像宽和高,注意这里的宽和高不是图像的分辨率 OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); // 将图像宽度和高度单位转化为像素单位 //#define HIMETRIC_PER_INCH 2540 //int nPicWidth = MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX),2540); //int nPicHeight = MulDiv(hmHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY),2540); //HRESULT Render( // HDC hdc, //Handle of device context on which to render the image // long x, //Horizontal position of image in hdc // long y, //Vertical position of image in hdc // long cx, //Horizontal dimension of destination rectangle // long cy, //Vertical dimension of destination rectangle // OLE_XPOS_HIMETRIC xSrc, //Horizontal offset in source picture // OLE_YPOS_HIMETRIC ySrc, //Vertical offset in source picture // OLE_XSIZE_HIMETRIC cxSrc, //Amount to copy horizontally in source picture // OLE_YSIZE_HIMETRIC cySrc, //Amount to copy vertically in source picture // LPCRECT prcWBounds //Pointer to position of destination for a metafile hdc // ); //use render function display image RECT rtWnd; pDC->GetWindow()->GetWindowRect(&rtWnd); int iWndWidth=rtWnd.right-rtWnd.left; int iWndHeight=rtWnd.bottom-rtWnd.top; if(FAILED(pPic->Render(*pDC,x,y,iWndWidth,iWndHeight,0,hmHeight,hmWidth,-hmHeight,NULL))) { pPic->Release(); GlobalFree(hGlobal); // 记得释放内存 return false; } pPic->Release(); GlobalFree(hGlobal); // 记得释放内存 return true; } //************************************ // 方法说明: 显示JPG和GIF、BMP图片 // 参数说明: CDC * pDC 设备环境对象 // 参数说明: CString strPath 要显示的图片路径 // 参数说明: int x 要显示的X位置 // 参数说明: int y 要显示的Y位置 // 返回值: BOOL 成功返回TRUE,否则返回FALSE //************************************ BOOL CShowJpgDlg::ShowImage(CDC* pDC,CString strPath, int x, int y) { IPicture *pPic=NULL; OleLoadPicturePath(CComBSTR(strPath.GetBuffer()), (LPUNKNOWN)NULL, 0, 0, IID_IPicture,(LPVOID*)&pPic); if (NULL==pPic) { return FALSE; } // 获取图像宽和高,注意这里的宽和高不是图像的分辨率 OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); // 将图像宽度和高度单位转化为像素单位 //#define HIMETRIC_PER_INCH 2540 //int nPicWidth = MulDiv(hmWidth, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSX),2540); //int nPicHeight = MulDiv(hmHeight, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY),2540); // 获取显示图片窗口的宽度和高度 RECT rtWnd; pDC->GetWindow()->GetWindowRect(&rtWnd); int iWndWidth=rtWnd.right-rtWnd.left; int iWndHeight=rtWnd.bottom-rtWnd.top; if(FAILED(pPic->Render(*pDC,x,y,iWndWidth,iWndHeight,0,hmHeight,hmWidth,-hmHeight,NULL))) { pPic->Release(); return false; } //记得释放资源,不然会导致内存泄露 pPic->Release(); return true; } //************************************ // 方法说明: 浏览图片 // 返回值: void //************************************ void CShowJpgDlg::OnBnClickedBtnBrowse() { // TODO: Add your control notification handler code here CFileDialog dlg(TRUE,"jpg","*.jpg", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif|bmp文件(*.bmp)|*.bmp|",NULL); if(dlg.DoModal()==IDOK) { SetDlgItemText(IDC_TXT_PATH,dlg.GetPathName()); //设置静态控件的样式,使其可以使用位图,并使位图显示居中 ((CStatic*)GetDlgItem(IDC_STATIC_IMG))-> ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE); CDC *pDC=NULL; pDC=GetDlgItem(IDC_STATIC_IMG)->GetDC(); //ShowJpgGif(pDC,dlg.GetPathName(),0,0); ShowImage(pDC,dlg.GetPathName(),0,0); ReleaseDC(pDC); // 记得释放资源,不然会导致内存泄露 } }
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
例子工程(VS2010)下载地址:http://download.csdn.net/detail/friendan/7709595
-----------------------------------------------------------------------------------------------------
相关文章推荐
- 动态规划-3018
- kafka理论
- spark 累加历史 + 统计全部 + 行转列
- 截图工具(window 10 和Mac OSX)
- vnc 在 rhel 6.x 上的配置及使用
- 病毒分析,病毒原理,ASLR,DEP,EPO
- Nginx负载均衡配置实例详解
- MySQL优化必须调整的10项配置
- 最全防止sql注入方法
- 第8课:Spark Streaming源码解读之RDD生成全生命周期彻底研究和思考
- 安卓UI设计中fill_parent、wrap_content和match_parent的区别
- FFmpeg之零概述(待续)
- 适配主流厂商浏览器插件Autoprefix css
- session超时时间设置方法
- C/C++内存泄漏及其检测工具
- MAC端Android反编译工具及使用
- 红黑树的实现
- SQL SERVER性能优化-查询速度提高
- php通过$_SERVER['HTTP_USER_AGENT']获取浏览器相关参数
- 自己写的基于JavaScript DOM 仿一些页面的鼠标移动显示相应区块内容