`
jgsj
  • 浏览: 962473 次
文章分类
社区版块
存档分类
最新评论

CxImage的简单用法

 
阅读更多

转自:http://blog.csdn.net/rackyye/archive/2008/03/04/2147170.aspx

相信大家在进行图形相关开发时,对CxImage一定非常熟悉,这是一个专门处理各种格式图形文件的C++库。我们在用OpenGL开发程序的时候,必然需要用到材质,对于简单的bmp格式,我们当然很轻松就能处理,但是对于像jpg,gif,tif,png,ico,pcx,tga等复杂格式,我们是不是就感觉很为难呢?其实用CxImage就能很轻松地处理这些格式文件。下面说一下CxImage的基本用法:
1、打开一张图
可以通过创建一个新的CxImage对象来完成,通过构造函数来打开一张图
CxImage::CxImage(const char * filename, DWORD imagetype)
其中filename是需要打开的文件路径,imagetype是文件类型,支持的类型有:
CXIMAGE_FORMAT_UNKNOWN,CXIMAGE_FORMAT_BMP,CXIMAGE_FORMAT_GIF,CXIMAGE_FORMAT_JPG,CXIMAGE_FORMAT_PNG,CXIMAGE_FORMAT_MNG,CXIMAGE_FORMAT_ICO,CXIMAGE_FORMAT_TIF,CXIMAGE_FORMAT_TGA,CXIMAGE_FORMAT_PCX,CXIMAGE_FORMAT_WBMP,CXIMAGE_FORMAT_WMF,CXIMAGE_FORMAT_J2K,CXIMAGE_FORMAT_JBG,CXIMAGE_FORMAT_JP2,CXIMAGE_FORMAT_JPC,CXIMAGE_FORMAT_PGX,CXIMAGE_FORMAT_PNM,CXIMAGE_FORMAT_RAS,
当然,这么多格式很难记住,我们可以通过如下函数来直接获得文件的格式。
int FindType(const CString& filename)
{
CString ext = filename.Right(filename.GetLength()-filename.ReverseFind('.')-1);
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_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;
}

2、保存一张图
bool CxImage::Save(LPCWSTR filename, DWORD imagetype=0)
参数和上面是一样的。
3、得到图形数据,以便在OpenGL中使用材质
BYTE* CxImage::GetBits(DWORD row = 0);
4、得到图形大小
long GetSize();
5、得到图形高度和宽度
DWORD CxImage::GetHeight();
DWORD CxImage::GetWidth();
6、得到文件类型
DWORD CxImage::GetType() const;
7、得到最后一个错误
char* CxImage::GetLastError();
8、在界面中绘制出来
long CxImage::Draw(HDC hdc, const RECT& rect, RECT* pClipRect=NULL)
HDC 绘图设备,rect 绘图的区域,确定绘图的左上角和右下角坐标。pClipRect,裁剪区域,一般可以和绘图区域一样大小,除非特殊需要。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics