1
MFC图像处理
上传图片
这里上传图片之后直接显示了,调用了自定义的显示图片的方法 ShowImage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| void CImageViewView::OnBnClickedButtonAddImg() { CFileDialog fileDlg(TRUE, _T("png"), NULL, 0, _T("image Files(*.bmp; *.jpg;*.png)|*.JPG;*.PNG;*.BMP|All Files (*.*) |*.*||"), this); if (fileDlg.DoModal() == IDCANCEL) return; CString strFilePath = fileDlg.GetPathName(); CString strFileName = fileDlg.GetFileName(); OutputDebugString(strFilePath+"文件路径为:【】【】【】"); if (strFilePath == _T("")) { return; }
if (!CopyFileToDirectory(strFilePath, m_strDestinationDir)) { return; }
ShowImage(m_strDestinationDir + "\\" + strFileName);
}
|
显示图片
通过画布的方式显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| void CImageViewView::ShowImage(CString strFilePath) { if (strFilePath == _T("")) { return; } CImage image; if (!image.IsNull()) image.Destroy(); image.Load(strFilePath);
CRect rectControl; CWnd* pWnd = GetDlgItem(IDC_STATIC_IMG_VIEW); pWnd->GetClientRect(&rectControl);
if (pWnd != NULL) { CRect rect; pWnd->GetClientRect(&rect); CDC* pDC = pWnd->GetDC(); if (pDC != NULL) { pDC->FillSolidRect(&rect, RGB(255, 255, 255)); pWnd->ReleaseDC(pDC); } }
CDC* pDc = GetDlgItem(IDC_STATIC_IMG_VIEW)->GetDC(); SetStretchBltMode(pDc->m_hDC, STRETCH_HALFTONE);
image.Draw(pDc->m_hDC, rectControl);
image.Destroy(); pWnd->ReleaseDC(pDc); }
|
复制文件
复制图片到指定路径下,用于更好的显示图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| BOOL CImageViewView::CopyFileToDirectory(CString strSourceFilePath, CString strDestinationDir) { if (!PathIsDirectory(strDestinationDir)) { CreateDirectory(strDestinationDir, NULL); }
CString strFileName = PathFindFileName(strSourceFilePath);
CString strDestFilePath = strDestinationDir + _T("\\") + strFileName;
if (!CopyFile(strSourceFilePath, strDestFilePath, FALSE)) { DWORD dwError = GetLastError(); CString strError; strError.Format(_T("文件上传失败,错误代码:%d"), dwError); AfxMessageBox(strError); return FALSE; } return TRUE; }
|