MFC显示JPG,bmp图片
?
主要代碼如下:
//************************************
// 方法說明: ? ?顯示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);?? ?// 記得釋放資源,不然會導致內存泄露
? ? }?
?
}
?
總結
以上是生活随笔為你收集整理的MFC显示JPG,bmp图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: awk 中 {print $1} 什么意
- 下一篇: 执行ssh-add时出现Could no