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;
}


具体实例打开一幅图片:

void CProDlg::OnBnClickedButton3()
{
CString strPicPath;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("图片文件(*.jpg;*.jpeg;*.gif;,*.bmp)|*.jpg;*.jpeg;*.gif;*.bmp|位图文件(*.BMP)|*.BMP||"));
dlg.m_ofn.lpstrInitialDir=_T(".//");
if(IDOK==dlg.DoModal())
{strPicPath.Format(_T("%s"),dlg.GetPathName());}
CString fileExt;
int len=strPicPath.GetLength();
for(int i=len-1;i>=0;i--)
{if(strPicPath[i]=='.'){fileExt=strPicPath.Mid(i+1);break;}}
fileExt.MakeLower();
int type;
if(fileExt!=_T(""))
{type=FindType(strPicPath);}
CxImage image;
image.Load(strPicPath,type);
//将整个控件调整为与图像同一尺寸
GetDlgItem(IDC_PIC1)->SetWindowPos(NULL,0,0,300,300,SWP_NOMOVE);
CRect zcRect;
GetDlgItem(IDC_PIC1)->GetClientRect(&zcRect);
CDC *pDC=GetDlgItem(IDC_PIC1)->GetDC();
image.Draw(pDC->m_hDC,zcRect.left,zcRect.top,300,300);

}


2、保存一张图

bool CxImage::Save(LPCWSTR filename, DWORD imagetype=0)


参数和上面是一样的。

具体实例;

void CProDlg::OnBnClickedButton1()
{ float scale=0.5;
CxImage image,smallImg;
CString fileName="d://1.jpg";
CString fileExt;
int len=fileName.GetLength();
for(int i=len-1;i>=0;i--)
{if(fileName[i]=='.'){fileExt=fileName.Mid(i+1);break;}}
fileExt.MakeLower();
int type;
if(fileExt!=_T(""))
{type=CxImage::GetTypeIdFromName(fileExt);}
image.Load(fileName);
image.Resample(image.GetWidth()*scale,image.GetHeight()*scale,1,&smallImg);
smallImg.Save("d://2.jpg",type);
}




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,裁剪区域,一般可以和绘图区域一样大小,除非特殊需要。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值