博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MFC显示JPG,bmp图片
阅读量:4302 次
发布时间:2019-05-27

本文共 4436 字,大约阅读时间需要 14 分钟。

 

主要代码如下:

//************************************

// 方法说明:    显示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);    // 记得释放资源,不然会导致内存泄露
    } 
 
}

 

转载地址:http://zgows.baihongyu.com/

你可能感兴趣的文章
linux进程之间通讯常用信号
查看>>
main函数带参数
查看>>
PCB布线技巧
查看>>
关于PCB设计中过孔能否打在焊盘上的两种观点
查看>>
PCB反推理念
查看>>
京东技术架构(一)构建亿级前端读服务
查看>>
git 提示:error: unable to rewind rpc post data - try increasing http.postBuffer
查看>>
php 解决json_encode中文UNICODE转码问题
查看>>
LNMP 安装 thinkcmf提示404not found
查看>>
PHP empty、isset、innull的区别
查看>>
apache+nginx 实现动静分离
查看>>
通过Navicat远程连接MySQL配置
查看>>
phpstorm开发工具的设置用法
查看>>
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>