在MFC Picture控件中显示图像

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ab1271173712/article/details/84374367

CWnd*      m_pWnd;  

m_pWnd = this->GetDlgItem(IDC_PICTURE);  //  IDC_PICTURE此为Picture控件ID 

  //********* 加载图像 ********************

   CString filter;
    filter = "支持的图像(*.bmp;*.tif;*.jpg)|*.bmp;*.tif;*.jpg||";
    CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter);

    //**默认加载图像时路径,此处可不做处理  

    CString  strForlder=TEXT("d:// "); 
    dlg.m_ofn.lpstrInitialDir = strForlder;    

   //**

    if (dlg.DoModal() == IDOK)
    {     
        try{

            // 此类方法即便实现体在OnPaint()外,Picture也可以显示图像,已经过验证
            CRect   rect;
            CImage  image;
            image.Load(dlg.GetPathName().GetBuffer());
            wndDraw->GetWindowRect(&rect);  //将客户区选中到控件表示的矩形区域内  
            CWnd *pWnd = NULL;
            pWnd = wndDraw;//获取控件句柄  
            pWnd->GetClientRect(&rect);//获取句柄指向控件区域的大小  
            CDC *pDc = NULL;
            pDc = pWnd->GetDC();//获取picture的DC  
            pDc->SetStretchBltMode(STRETCH_HALFTONE);
            image.Draw(pDc->m_hDC, rect);//将
            ReleaseDC(pDc);

        }
         catch(CException *e)//&e
        {
             TCHAR   szError[1024];   

             e->GetErrorMessage(szError,1024);   //  e.GetErrorMessage(szError,1024); 
             ::AfxMessageBox(szError); 
        } 

    }

猜你喜欢

转载自blog.csdn.net/ab1271173712/article/details/84374367