MFC动态加载图片

  1. void CMFCDemoDlg::OnInsertImage()  
  2. {  
  3.     CFileDialog dlg(TRUE,_T(”jpg”),_T(“*.jpg”), OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,     
  4.         _T(”JPEG文件(*.jpg)|*.jpg|GIF文件(*.gif)|*.gif|bmp文件(*.bmp)|*.bmp|”), NULL);  
  5.   
  6.     if(dlg.DoModal()!=IDOK)  
  7.         return;  
  8.       
  9.     ((CStatic*)GetDlgItem(IDC_STATIC_PICTURE))-> ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE);   
  10.       
  11.     m_strImgPath = dlg.GetPathName();  
  12.   
  13.     CDC *pDC=NULL;    
  14.     pDC = GetDlgItem(IDC_STATIC_PICTURE)->GetDC();    
  15.     DrawImg(pDC, m_strImgPath, 0, 0);    
  16.   
  17.     ReleaseDC(pDC); // 记得释放资源,不然会导致内存泄露    
  18. }  
  19.   
  20. void CMFCDemoDlg::DrawImg(CDC* pDC,CString strPath, int x, int y)  
  21. {  
  22.     IPicture *pPic=NULL;     
  23.     OleLoadPicturePath(CComBSTR(strPath.GetBuffer()), (LPUNKNOWN)NULL, 0, 0, IID_IPicture,(LPVOID*)&pPic);    
  24.     if (NULL == pPic)    
  25.     {    
  26.         return;    
  27.     }    
  28.   
  29.     // 获取图像宽和高,注意这里的宽和高不是图像的分辨率    
  30.     OLE_XSIZE_HIMETRIC hmWidth;      
  31.     OLE_YSIZE_HIMETRIC hmHeight;      
  32.     pPic->get_Width(&hmWidth);      
  33.     pPic->get_Height(&hmHeight);      
  34.   
  35.     // 获取显示图片窗口的宽度和高度    
  36.     RECT rtWnd;    
  37.     pDC->GetWindow()->GetWindowRect(&rtWnd);    
  38.     int iWndWidth = rtWnd.right - rtWnd.left;    
  39.     int iWndHeight = rtWnd.bottom - rtWnd.top;  
  40.     if(m_bPrintFlag)  
  41.     {  
  42.         iWndHeight = iWndHeight * 10;  
  43.         iWndWidth = iWndWidth * 10;  
  44.     }  
  45.   
  46.     if(FAILED(pPic->Render(*pDC, x, y, iWndWidth, iWndHeight, 0, hmHeight, hmWidth, -hmHeight, NULL)))      
  47.     {    
  48.         pPic->Release();    
  49.         return;    
  50.     }    
  51.   
  52.     //记得释放资源,不然会导致内存泄露    
  53.     pPic->Release();     
  54.   
  55.     return;    
  56. }  
void CMFCDemoDlg::OnInsertImage()
{
    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)
        return;

    ((CStatic*)GetDlgItem(IDC_STATIC_PICTURE))-> ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE); 

    m_strImgPath = dlg.GetPathName();

    CDC *pDC=NULL;  
    pDC = GetDlgItem(IDC_STATIC_PICTURE)->GetDC();  
    DrawImg(pDC, m_strImgPath, 0, 0);  

    ReleaseDC(pDC); // 记得释放资源,不然会导致内存泄露  
}

void CMFCDemoDlg::DrawImg(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;  
    }  

    // 获取图像宽和高,注意这里的宽和高不是图像的分辨率  
    OLE_XSIZE_HIMETRIC hmWidth;    
    OLE_YSIZE_HIMETRIC hmHeight;    
    pPic->get_Width(&hmWidth);    
    pPic->get_Height(&hmHeight);    

    // 获取显示图片窗口的宽度和高度  
    RECT rtWnd;  
    pDC->GetWindow()->GetWindowRect(&rtWnd);  
    int iWndWidth = rtWnd.right - rtWnd.left;  
    int iWndHeight = rtWnd.bottom - rtWnd.top;
    if(m_bPrintFlag)
    {
        iWndHeight = iWndHeight * 10;
        iWndWidth = iWndWidth * 10;
    }

    if(FAILED(pPic->Render(*pDC, x, y, iWndWidth, iWndHeight, 0, hmHeight, hmWidth, -hmHeight, NULL)))    
    {  
        pPic->Release();  
        return;  
    }  

    //记得释放资源,不然会导致内存泄露  
    pPic->Release();   

    return;  
}

            </div>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值