您的位置:首页 > 其它

MFC显示JPG、JIF图片

2017-05-27 11:24 351 查看
链接:http://blog.csdn.net/friendan/article/details/38358507

注:此代码不存在内存泄露,很好的代码

控件ID:
picture控件:IDC_STATIC_IMG
路径编辑框:IDC_TXT_PATH


界面如下图所示:



//************************************
// 方法说明:    显示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); // 记得释放资源,不然会导致内存泄露
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: