CImage 属于MFC和STL共享的类库里的,为图像处理提供了许多方法。
CImage类概述
CImage是MFC和ATL共享的新类,它能从外部磁盘中调入一个JPEG、GIF、BMP和PNG格式的图像文件加以显示,而且这些文件格式可以 相互转换。由于CImage在不同的Windows操作系统中其某些性能是不一样的,因此在使用时要特别注意。例如,CImage::PlgBlt和 CImage::MaskBlt只能在 Windows NT 4.0 或更高版本中使用,但不能运行在Windows 95/98 应用程序中。CImage::AlphaBlend和CImage::TransparentBlt也只能在
Windows 2000/98或其更高版本中使用。即使在Windows 2000运行程序还必须将stdafx.h文件中的WINVER和_WIN32_WINNT的预定义修改成0x0500才能正常使用。
使用CImage的一般方法是这样的过程:
(1) 打开应用程序的stdafx.h文件添加CImage类的包含文件:
#include<atlimage.h>
(2) 定义一个CImage类对象,然后调用CImage::Load方法装载一个外部图像文件。
(3) 调用CImage::Draw方法绘制图像。
void CImageProcessView::OnFileOpen()
{
// TODO: 在此添加命令处理程序代码
CString strFilter;
CSimpleArray<GUID> aguidFileTypes;
HRESULT hResult;
// 获取CImage支持的图像文件的过滤字符串
hResult = m_Image.GetExporterFilterString(strFilter,aguidFileTypes, _T( "All Image Files") );
if (FAILED(hResult))
{
MessageBox(_T("GetExporterFilter调用失败!"));
return;
}
CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
if(IDOK != dlg.DoModal())
return;
m_Image.Destroy(); // 将外部图像文件装载到CImage对象中 hResult = m_Image.Load(dlg.GetPathName());//许多教程上都写错了 都写的是GetFileName() if (FAILED(hResult))
{ MessageBox(_T("调用图像文件失败!")); return; }
// 设置主窗口标题栏内容 CString str; str.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str + _T(" - ") +dlg.GetFileName()); Invalidate(); // 强制调用OnDraw }
下面解释一下CSimpleArray<GUID>函数的意义,//获取CImage支持的图像文件的过滤字符串
CSimpleArray是用于管理一个简单数组的方法。
先看一个实例:
// Create an array of integers
CSimpleArray<int> iArray;
// Create an array of char pointers
// and use a new equality function
CSimpleArray<char *, MyEqualityEqualHelper<char *> > cMyArray;