在MFC中有一个控件picture control控件,利用该控件可以显示图像。在这里利用CxImage库、MFC中的picture Control控件显示图像的方法和步骤如下:
1、新建基于对话框的MFC界面程序,在工程中配置CxImage库。
配置之后的结果如下:
同样在release版本下配置,不过引用路径要改成Release文件夹。
同样在release版本下配置,添加的lib为cximage.dll文件。
2、在工程中Dlg.h即对话框类的声明中添加两个成员:
///////以下为自己定义的类成员
public:
CWnd* m_pWnd0; //显示窗口1
CWnd* m_pWnd1; //显示窗口2
CxImage* m_pImage1; //窗口1图像显示类库指针
CxImage* m_pBKImage; //窗口2显示类指针
并在类的OnInitDialog函数中将指针置为NULL。
并在类的析构函数中添加CxImage指针的内存释放语句
//....
if(m_pImage1 != NULL)
{
delete m_pImage1;
m_pImage1 = NULL;
}
//...
3、为类添加以下三个成员函数:
/* 获取文件的扩展名*/
CString CImageShowDlg::FindExtension(const CString &name)
{
int len = name.GetLength();
int i;
for (i = len-1; i >= 0; i--){
if (name[i] == '.'){
return name.Mid(i+1);
}
}
return CString("");
}
/*获取图像扩展名的图像类型*/
int CImageShowDlg::FindType(const CString &ext)
{
int type = 0;
if (ext == "bmp") type = CXIMAGE_FORMAT_BMP;
#if CXIMAGE_SUPPORT_JPG
else if (ext=="jpg"||ext=="jpeg") type = CXIMAGE_FORMAT_JPG;
#endif
#if CXIMAGE_SUPPORT_JPG
else if (ext=="JPG"||ext=="JPEG") type = CXIMAGE_FORMAT_JPG;
#endif
#if CXIMAGE_SUPPORT_GIF
else if (ext == "gif") type = CXIMAGE_FORMAT_GIF;
#endif
#if CXIMAGE_SUPPORT_PNG
else if (ext == "png") type = CXIMAGE_FORMAT_PNG;
#endif
#if CXIMAGE_SUPPORT_MNG
else if (ext=="mng"||ext=="jng") type = CXIMAGE_FORMAT_MNG;
#endif
#if CXIMAGE_SUPPORT_ICO
else if (ext == "ico") type = CXIMAGE_FORMAT_ICO;
#endif
#if CXIMAGE_SUPPORT_TIF
else if (ext=="tiff"||ext=="tif") type = CXIMAGE_FORMAT_TIF;
#endif
#if CXIMAGE_SUPPORT_TGA
else if (ext=="tga") type = CXIMAGE_FORMAT_TGA;
#endif
#if CXIMAGE_SUPPORT_PCX
else if (ext=="pcx") type = CXIMAGE_FORMAT_PCX;
#endif
#if CXIMAGE_SUPPORT_WBMP
else if (ext=="wbmp") type = CXIMAGE_FORMAT_WBMP;
#endif
#if CXIMAGE_SUPPORT_WMF
else if (ext=="wmf"||ext=="emf") type = CXIMAGE_FORMAT_WMF;
#endif
#if CXIMAGE_SUPPORT_J2K
else if (ext=="j2k"||ext=="jp2") type = CXIMAGE_FORMAT_J2K;
#endif
#if CXIMAGE_SUPPORT_JBG
else if (ext=="jbg") type = CXIMAGE_FORMAT_JBG;
#endif
#if CXIMAGE_SUPPORT_JP2
else if (ext=="jp2"||ext=="j2k") type = CXIMAGE_FORMAT_JP2;
#endif
#if CXIMAGE_SUPPORT_JPC
else if (ext=="jpc"||ext=="j2c") type = CXIMAGE_FORMAT_JPC;
#endif
#if CXIMAGE_SUPPORT_PGX
else if (ext=="pgx") type = CXIMAGE_FORMAT_PGX;
#endif
#if CXIMAGE_SUPPORT_RAS
else if (ext=="ras") type = CXIMAGE_FORMAT_RAS;
#endif
#if CXIMAGE_SUPPORT_PNM
else if (ext=="pnm"||ext=="pgm"||ext=="ppm") type = CXIMAGE_FORMAT_PNM;
#endif
else type = CXIMAGE_FORMAT_UNKNOWN;
return type;
}
这两个函数主要为了根据图像的路径获取图像的类别信息,方便后面的图像的读取。
//绘制图像函数
void CImageShowDlg::DrawImgOnCtrl(CxImage* pImg , CWnd* wndDraw)
{
int nImgWidth,nImgHeight; //图像的长宽
CRect rectWndDraw; //绘图窗口的矩形
CRect rectDispaly; //图像显示区域
//获取图像的长宽
nImgWidth = pImg->GetWidth();
nImgHeight = pImg->GetHeight();
//获取绘制窗口的长宽
wndDraw->GetClientRect(&rectWndDraw);
//根据图像和绘制区域的比例绘制图像
CDC* pDC;
pDC = wndDraw->GetDC(); //获取窗口句柄
pDC->FillSolidRect(rectWndDraw,RGB(0,0,0));
//绘制图像到窗口区域
float xRadio,yRadio;
float fRadio;
xRadio = (float)rectWndDraw.Width()/(float)nImgWidth;
yRadio = (float)rectWndDraw.Height()/(float)nImgHeight;
if(xRadio >= yRadio)
fRadio = yRadio;
else
fRadio = xRadio;
//计算图像显示的位置
rectDispaly.left = int((rectWndDraw.Width() - nImgWidth * fRadio)/2) + rectWndDraw.left;
rectDispaly.top = int((rectWndDraw.Height() - nImgHeight * fRadio)/2) + rectWndDraw.top;
rectDispaly.right = int(rectDispaly.left + nImgWidth * fRadio);
rectDispaly.bottom = int(rectDispaly.top + nImgHeight * fRadio);
pImg->Draw(pDC->GetSafeHdc(),rectDispaly);
}
这个函数主要功能是:在picture control控件中根据
picture control控件的大小动态显示图片(即无论图像多大,都可以在控件中显示,缩放显示)。
4、在工程的资源视图中Dialog主对话框添加两个pictrue control控件和2个按钮,如下:
并修改这四个控件的ID,并记住(后面要用到)。
5、在两个按钮的消息相应函数中添加代码:(这里只放上一个按钮的响应函数,另一个一样,只是相关的ID和指针不一样。)
void CImageShowDlg::OnBnClickedButtonTu2()
{
// TODO: 在此添加控件通知处理程序代码
//打开图片选择对话框
LPCTSTR szFilter = _T("BMP(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg|ALLSUPORTFILE(*.*)|*.*||");
CFileDialog dlgBKFile(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter,NULL);
CString strBKFileName;
//如果按下OK键
if(dlgBKFile.DoModal() == IDOK)
{
//读取视频文件
strBKFileName = dlgBKFile.GetPathName();
}
else
{
return;
}
//建立CxImage的类指针
CString strExt; //获取文件后缀名
int imageType; //获取图像类型
strExt = FindExtension(strBKFileName);
imageType = FindType(strExt);
//判断指针是否为空
if(m_pImage2 != NULL)
{
delete m_pImage2;
m_pImage2 = NULL;
}
//开辟内存
m_pImage2 = new CxImage();
//打开图片
m_pImage2->Load(strBKFileName, imageType);
if(!m_pImage2->IsValid())
{
AfxMessageBox(_T("建立图像指针失败!"));
delete m_pImage2;
m_pBmage2 = NULL;
return;
}
//绘制图像到相应的图像控件上
m_pWnd2 = this->GetDlgItem(IDC_STATIC_Tu2); //这里一定要和picture Control控件的ID对应起来
DrawImgOnCtrl(m_pImage2,m_pWnd2);
}
前两句语句,主要是为了在单击该按钮的时候,系统会弹出选择文件的对话框。
6、运行结果如下:
打开图像后显示如下:
注意:如果需要的工程的Debug或则Release文件夹下直接运行生成的exe文件,则需要将第一步中的那个dll分别拷贝到Debug或者是Release文件夹下,才可运行。