CxImage动态加载图片(判断图片文件类型)

简介: 1、打开一张图可以通过创建一个新的CxImage对象来完成,通过构造函数来打开一张图CxImage::CxImage(const char * filename, DWORD imagetype)其中filename是需要打开的文件路径,imagetype是文件类型,支持的类型有: [c...

1、打开一张图
可以通过创建一个新的CxImage对象来完成,通过构造函数来打开一张图
CxImage::CxImage(const char * filename, DWORD imagetype)
其中filename是需要打开的文件路径,imagetype是文件类型,支持的类型有:

[cpp]  view plain  copy
 
  1. CXIMAGE_FORMAT_UNKNOWN,  
  2. CXIMAGE_FORMAT_BMP,  
  3. CXIMAGE_FORMAT_GIF,  
  4. CXIMAGE_FORMAT_JPG,  
  5. CXIMAGE_FORMAT_PNG,  
  6. CXIMAGE_FORMAT_MNG,  
  7. CXIMAGE_FORMAT_ICO,  
  8. CXIMAGE_FORMAT_TIF,  
  9. CXIMAGE_FORMAT_TGA,  
  10. CXIMAGE_FORMAT_PCX,  
  11. CXIMAGE_FORMAT_WBMP,  
  12. CXIMAGE_FORMAT_WMF,  
  13. CXIMAGE_FORMAT_J2K,  
  14. CXIMAGE_FORMAT_JBG,  
  15. CXIMAGE_FORMAT_JP2,  
  16. CXIMAGE_FORMAT_JPC,  
  17. CXIMAGE_FORMAT_PGX,  
  18. CXIMAGE_FORMAT_PNM,  
  19. CXIMAGE_FORMAT_RAS,  


当然,这么多格式很难记住,我们可以通过如下函数来直接获得文件的格式

[cpp]  view plain  copy
 
  1. int FindType(const CString& filename)  
  2. {  
  3. CString ext = filename.Right(filename.GetLength()-filename.ReverseFind('.')-1);  
  4. int type = 0;  
  5. if (ext == "bmp") type = CXIMAGE_FORMAT_BMP;  
  6. #if CXIMAGE_SUPPORT_JPG  
  7. else if (ext=="jpg"||ext=="jpeg") type = CXIMAGE_FORMAT_JPG;  
  8. #endif  
  9. #if CXIMAGE_SUPPORT_GIF  
  10. else if (ext == "gif") type = CXIMAGE_FORMAT_GIF;  
  11. #endif  
  12. #if CXIMAGE_SUPPORT_PNG  
  13. else if (ext == "png") type = CXIMAGE_FORMAT_PNG;  
  14. #endif  
  15. #if CXIMAGE_SUPPORT_MNG  
  16. else if (ext=="mng"||ext=="jng") type = CXIMAGE_FORMAT_MNG;  
  17. #endif  
  18. #if CXIMAGE_SUPPORT_ICO  
  19. else if (ext == "ico") type = CXIMAGE_FORMAT_ICO;  
  20. #endif  
  21. #if CXIMAGE_SUPPORT_TIF  
  22. else if (ext=="tiff"||ext=="tif") type = CXIMAGE_FORMAT_TIF;  
  23. #endif  
  24. #if CXIMAGE_SUPPORT_TGA  
  25. else if (ext=="tga") type = CXIMAGE_FORMAT_TGA;  
  26. #endif  
  27. #if CXIMAGE_SUPPORT_PCX  
  28. else if (ext=="pcx") type = CXIMAGE_FORMAT_PCX;  
  29. #endif  
  30. #if CXIMAGE_SUPPORT_WBMP  
  31. else if (ext=="wbmp") type = CXIMAGE_FORMAT_WBMP;  
  32. #endif  
  33. #if CXIMAGE_SUPPORT_WMF  
  34. else if (ext=="wmf"||ext=="emf") type = CXIMAGE_FORMAT_WMF;  
  35. #endif  
  36. #if CXIMAGE_SUPPORT_J2K  
  37. else if (ext=="j2k"||ext=="jp2") type = CXIMAGE_FORMAT_J2K;  
  38. #endif  
  39. #if CXIMAGE_SUPPORT_JBG  
  40. else if (ext=="jbg") type = CXIMAGE_FORMAT_JBG;  
  41. #endif  
  42. #if CXIMAGE_SUPPORT_JP2  
  43. else if (ext=="jp2"||ext=="j2k") type = CXIMAGE_FORMAT_JP2;  
  44. #endif  
  45. #if CXIMAGE_SUPPORT_JPC  
  46. else if (ext=="jpc"||ext=="j2c") type = CXIMAGE_FORMAT_JPC;  
  47. #endif  
  48. #if CXIMAGE_SUPPORT_PGX  
  49. else if (ext=="pgx") type = CXIMAGE_FORMAT_PGX;  
  50. #endif  
  51. #if CXIMAGE_SUPPORT_RAS  
  52. else if (ext=="ras") type = CXIMAGE_FORMAT_RAS;  
  53. #endif  
  54. #if CXIMAGE_SUPPORT_PNM  
  55. else if (ext=="pnm"||ext=="pgm"||ext=="ppm") type = CXIMAGE_FORMAT_PNM;  
  56. #endif  
  57. else type = CXIMAGE_FORMAT_UNKNOWN;  
  58.   
  59. return type;  
  60. }  


具体实例打开一幅图片:

[cpp]  view plain  copy
 
  1. void CProDlg::OnBnClickedButton3()  
  2. {  
  3. CString strPicPath;  
  4. CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("图片文件(*.jpg;*.jpeg;*.gif;,*.bmp)|*.jpg;*.jpeg;*.gif;*.bmp|位图文件(*.BMP)|*.BMP||"));  
  5. dlg.m_ofn.lpstrInitialDir=_T(".//");  
  6. if(IDOK==dlg.DoModal())  
  7. {strPicPath.Format(_T("%s"),dlg.GetPathName());}  
  8. CString fileExt;  
  9. int len=strPicPath.GetLength();  
  10. for(int i=len-1;i>=0;i--)  
  11. {if(strPicPath[i]=='.'){fileExt=strPicPath.Mid(i+1);break;}}  
  12. fileExt.MakeLower();  
  13. int type;  
  14. if(fileExt!=_T(""))  
  15. {type=FindType(strPicPath);}  
  16. CxImage image;  
  17. image.Load(strPicPath,type);  
  18. //将整个控件调整为与图像同一尺寸  
  19. GetDlgItem(IDC_PIC1)->SetWindowPos(NULL,0,0,300,300,SWP_NOMOVE);  
  20. CRect zcRect;  
  21. GetDlgItem(IDC_PIC1)->GetClientRect(&zcRect);  
  22. CDC *pDC=GetDlgItem(IDC_PIC1)->GetDC();  
  23. image.Draw(pDC->m_hDC,zcRect.left,zcRect.top,300,300);  
  24.   
  25. }  


2、保存一张图

[cpp]  view plain  copy
 
  1. bool CxImage::Save(LPCWSTR filename, DWORD imagetype=0)  


参数和上面是一样的。

具体实例;

[cpp]  view plain  copy
 
  1. void CProDlg::OnBnClickedButton1()  
  2. float scale=0.5;  
  3. CxImage image,smallImg;  
  4. CString fileName="d://1.jpg";  
  5. CString fileExt;  
  6. int len=fileName.GetLength();  
  7. for(int i=len-1;i>=0;i--)  
  8. {if(fileName[i]=='.'){fileExt=fileName.Mid(i+1);break;}}  
  9. fileExt.MakeLower();  
  10. int type;  
  11. if(fileExt!=_T(""))  
  12. {type=CxImage::GetTypeIdFromName(fileExt);}  
  13. image.Load(fileName);  
  14. image.Resample(image.GetWidth()*scale,image.GetHeight()*scale,1,&smallImg);  
  15. smallImg.Save("d://2.jpg",type);  
  16. }  




3、得到图形数据,以便在OpenGL中使用材质

[cpp]  view plain  copy
 
  1. BYTE* CxImage::GetBits(DWORD row = 0);  


4、得到图形大小

[cpp]  view plain  copy
 
  1. long GetSize();  


5、得到图形高度和宽度

[cpp]  view plain  copy
 
  1. DWORD CxImage::GetHeight();  
  2. DWORD CxImage::GetWidth();  


6、得到文件类型

[cpp]  view plain  copy
 
  1. DWORD CxImage::GetType() const;  


7、得到最后一个错误

[cpp]  view plain  copy
 
  1. char* CxImage::GetLastError();  


8、在界面中绘制出来

[cpp]  view plain  copy
 
  1. long CxImage::Draw(HDC hdc, const RECT& rect, RECT* pClipRect=NULL)  


HDC 绘图设备,rect 绘图的区域,确定绘图的左上角和右下角坐标。pClipRect,裁剪区域,一般可以和绘图区域一样大小,除非特殊需要。

目录
相关文章
|
8月前
livp图片怎么打开以及怎么转换成jpg格式教程
livp图片怎么打开以及怎么转换成jpg格式教程
|
存储 编解码 Java
图片Exif信息解析(Java实现)
图片Exif信息解析(Java实现)
|
Java 文件存储 Maven
将PDF文件转换成PNG图片
有这样一个业务场景:需要在 WEB 页面中浏览 PDF 文件,PDF 文件存储在 FTP 服务器上,即 PDF 文件对外提供的访问地址的协议是 ftp 的。有如下几个硬条件、软需求的要求: - WEB 页面本身的可视区域不是很大; - 不想弹出对话框展示 PDF 文件; - 谷歌浏览器不支持在 http 协议的页面里内嵌 ftp 协议的路径; - 在 WEB 页面中使用系统默认的 PDF 阅读器的体验不是很好,滚动条啦,边框啦。
278 0
|
存储 编解码 C语言
C语言代码创建、解析BMP格式图片
BMP格式的图片是众多图片格式中的一种,也称为位图数据,BMP结构也比较简单,不需要依赖任何外部库,直接手撸几十行代码即可完成解码编码,非常方便。
352 0
Halcon打开图片:单个图片以及遍历读取文件夹里的所有图片
Halcon打开图片:单个图片以及遍历读取文件夹里的所有图片
871 0
|
C# 移动开发
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
原文:将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小) WPF的XAML文档(Main.xaml):                                   CS代码:(Main.
997 0