- 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;
- }
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>