zoukankan      html  css  js  c++  java
  • CxImage动态加载图片(判断图片文件类型)

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

  • 相关阅读:
    打包成exe可执行文件的方法
    Html 重要单词归纳
    python基础---递归函数 知识点自查填空题
    python基础---内置函数 和 匿名函数 知识点自查填空题
    python基础---递归函数真题解析
    python 日记 day4。
    python 日记 day5 字典
    python 日记 day3
    python 日记 day4
    python 日记 day1
  • 原文地址:https://www.cnblogs.com/lidabo/p/6923408.html
Copyright © 2011-2022 走看看