已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
GDI+总结范文 GDI+总结一保存图像文件要将图像文件保存,必须先获得图像的编码格式信息,但是GDI+没有直接提供这个函数GetEncoderClsid(const WCHAR*format,CLSID*pClsid)因此需要我们自己写一个GetEncoderClsid取得图像编码格式的函数幸好,有GetImageDecoders函数作为参照cppview plaincopy1.#include2.#include3.#include4.using namespaceGdiplus;5.6.INT main()7.8./Initialize GDI+.9.GdiplusStartupInput gdiplusStartupInput;10.ULONG_PTR gdiplusToken;11.GdiplusStartup(&gdiplusToken,&gdiplusStartupInput,NULL);12.13.UINT num;/number ofimage decoders14.UINT size;/size,in bytes,of the image decoderarray15.16.ImageCodecInfo*pImageCodecInfo;17.18./How manydecoders arethere?19./How big(in bytes)is thearray ofall ImageCodecInfo objects?20.GetImageDecodersSize(&num,&size);21.22./Create abuffer largeenough tohold thearray of ImageCodecInfo23./objects thatwill bereturned byGetImageDecoders.24.pImageCodecInfo=(ImageCodecInfo*)(malloc(size);25.26./GetImageDecoders createsan arrayofImageCodecInfoobjects27./and copiesthat arrayinto apreviously allocatedbuffer.28./The thirdargument,imageCodecInfos,is apointer tothat buffer.29.GetImageDecoders(num,size,pImageCodecInfo);30.31./Display thegraphics fileformat(MimeType)32./for eachImageCodecInfoobject.33.for(UINT j=0;jSave(szPicFilePath,&clsid,&encoderParameters);35.delete bitmap;36./*37.pression=100;38.GetEncoderClsid(Limage/jpeg,&clsid);39.40.encoderParameters.Count=1;41.encoderParameters.Parameter0.Guid=EncoderQuality;42.encoderParameters.Parameter0.Type=EncoderParameterValueTypeLong;43.encoderParameters.Parameter0.NumberOfValues=1;44.encoderParameters.Parameter0.Value=&pression;45.*/46.GDI+总结二为什么说CImage类是基于GDI+的?在许多资料上都说CImage类是基于GDI+的,但是为什么是基于GDI+的呢?因为使用这个类时,并没有加入#include,也没有在程序开始和结束时分别写GDI+启动代码GdiplusStartupInput和结束代码GdiplusShutdown使用这个类时,仅仅需要添加头文件#include就可以了,比GDI+得使用要简单一些。 而CImage对图片的处理很类似GDI+,其内部是不是封装了GDI+呢?幸好,CImage类是源码公开的,我们可以研究其源码,以便加深理解。 首先,看看altimage.h头文件cppview plaincopy1.#ifndef_ATLIMAGE_H_2.#define_ATLIMAGE_H_3.4.#pragma once5.6.#include7.#include8.#include9.#include10.#include11.12.#ifndef_ATL_NO_PRAGMA_WARNINGS13.#pragma warning(push)14.#pragma warning(disable:4820)/padding addedafter member15.#endif/!_ATL_NO_PRAGMA_WARNINGS16.17.#pragma warning(push,3)18.#pragma push_macro(new)19.#undef new20.#include/注意这里添加了GDI+得头文件21.#pragma pop_macro(new)22.#pragma warning(pop)23.24.#include25.26.#ifndef_ATL_NO_DEFAULT_LIBS27.#pragma ment(lib,gdi32.lib)28.#pragma ment(lib,shlwapi.lib)29.#pragma ment(lib,gdiplus.lib)30.#if WINVER=0x050031.#pragma ment(lib,msimg32.lib)32.#endif/WINVER=0x050033.#endif/!_ATL_NO_DEFAULT_LIBS34.35.#pragma pack(push,_ATL_PACKING)上面包含了GDI+得头文件再来看CImage的定义cppview plaincopy1.class CImage2.3.private:4.class CDCCache5.6.public:7.CDCCache()throw();8.CDCCache()throw();9.10.HDC GetDC()throw();11.void ReleaseDC(HDC)throw();12.13.private:14.HDC m_ahDCsCIMAGE_DC_CACHE_SIZE;15.;16.17.class CInitGDIPlus18.19.public:20.CInitGDIPlus()throw();21.CInitGDIPlus()throw();22.23.bool Init()throw();24.void ReleaseGDIPlus()throw();25.void IncreaseCImageCount()throw();26.void DecreaseCImageCount()throw();27.28.private:29.ULONG_PTR m_dwToken;30.CRITICAL_SECTION m_sect;31.LONG m_nCImageObjects;32.;cppview plaincopy1.static CInitGDIPluss_initGDIPlus;cppview plaincopy1.static CDCCaches_cache;它定义了两个类成员变量其中CInitGDIPlus是负责GDI+的启动和释放我们再看一下,这个成员类的Init()方法cppview plaincopy1.inline boolCImage:CInitGDIPlus:Init()throw()2.3.EnterCriticalSection(&m_sect);4.bool fRet=true;5.if(m_dwToken=0)6.7.Gdiplus:GdiplusStartupInput input;8.Gdiplus:GdiplusStartupOutput output;9.Gdiplus:Status status=Gdiplus:GdiplusStartup(&m_dwToken,&input,&output);/启动GDI+10.if(status!=Gdiplus:Ok)11.fRet=false;12.13.LeaveCriticalSection(&m_sect);14.return fRet;15.也就是说使用这个函数启动GDI+再看下一个函数cppview plaincopy1.inline voidCImage:CInitGDIPlus:ReleaseGDIPlus()throw()2.3.EnterCriticalSection(&m_sect);4.if(m_dwToken!=0)5.6.Gdiplus:GdiplusShutdown(m_dwToken);7.8.m_dwToken=0;9.LeaveCriticalSection(&m_sect);10.也就是说,使用这一个函数,用来关闭GDI+到此,我们便可知道,CImage类是基于GDI+的,但是我们还不知道CImage对象是不是在初始化时就启动了GDI+?如果不是,那什么时候才启动GDI+呢?为解决这个疑惑,我们查看CImage构造函数cppview plaincopy1.inline CImage:CImage()throw():2.m_hBitmap(NULL),3.m_pBits(NULL),4.m_hDC(NULL),5.m_nDCRefCount (0),6.m_hOldBitmap(NULL),7.m_nWidth (0),8.m_nHeight (0),9.m_nPitch (0),10.m_nBPP (0),11.m_iTransparentColor(-1),12.m_bHasAlphaChannel(false),13.m_bIsDIBSection(false)14.15.s_initGDIPlus.IncreaseCImageCount();16.cppview plaincopy1.inline voidCImage:CInitGDIPlus:IncreaseCImageCount()throw()2.3.EnterCriticalSection(&m_sect);4.m_nCImageObjects+;5.LeaveCriticalSection(&m_sect);6.由此可见,构造函数并没有启动GDI+也就是说定义CImage image;这个image变量时,并没有启动GDI+我们继续查找cppview plaincopy1.inline boolCImage:InitGDIPlus()throw()2.3.bool bSuess=s_initGDIPlus.Init();4.return(bSuess);5.CImage使用InitGDIPlus()来初始化GDI+因此我们查找InitGDIPlus()的所有引用,发现以下函数cppview plaincopy1.inline HRESULTCImage:GetImporterFilterString(CSimpleString&strImporters,2.CSimpleArray&aguidFileTypes,LPCTSTR pszAllFilesDescription/*=NULL*/,3.DWORD dwExclude/*=excludeDefaultLoad*/,TCHAR chSeparator/*=|*/)4.5.if(!InitGDIPlus()6.7.return(E_FAIL);8.9.10.UINT nCodecs;11.UINT nSize;12.Gdiplus:Status status;13.Gdiplus:ImageCodecInfo*pCodecs;14.15.status=Gdiplus:GetImageDecodersSize(&nCodecs,&nSize);16.USES_ATL_SAFE_ALLOCA;17.pCodecs=static_cast(_ATL_SAFE_ALLOCA(nSize,_ATL_SAFE_ALLOCA_DEF_THRESHOLD);18.19.if(pCodecs=NULL)20.return E_OUTOFMEMORY;21.22.status=Gdiplus:GetImageDecoders(nCodecs,nSize,pCodecs);23.BuildCodecFilterString(pCodecs,nCodecs,strImporters,aguidFileTypes,pszAllFilesDescription,dwExclude,chSeparator);24.25.return(S_OK);26.27.28.inline HRESULTCImage:GetExporterFilterString(CSimpleString&strExporters,29.CSimpleArray&aguidFileTypes,LPCTSTR pszAllFilesDescription/*=NULL*/,30.DWORD dwExclude/*=excludeDefaultSave*/,TCHAR chSeparator/*=|*/)31.32.if(!InitGDIPlus()33.34.return(E_FAIL);35.36.37.UINT nCodecs;38.UINT nSize;39.Gdiplus:Status status;40.Gdiplus:ImageCodecInfo*pCodecs;41.42.status=Gdiplus:GetImageDecodersSize(&nCodecs,&nSize);43.USES_ATL_SAFE_ALLOCA;44.pCodecs=static_cast(_ATL_SAFE_ALLOCA(nSize,_ATL_SAFE_ALLOCA_DEF_THRESHOLD);45.46.if(pCodecs=NULL)47.return E_OUTOFMEMORY;48.49.status=Gdiplus:GetImageDecoders(nCodecs,nSize,pCodecs);50.BuildCodecFilterString(pCodecs,nCodecs,strExporters,aguidFileTypes,pszAllFilesDescription,dwExclude,chSeparator);51.52.return(S_OK);53.cppview plaincopy1.inline HRESULTCImage:Load(IStream*pStream)throw()2.3.if(!InitGDIPlus()4.5.return(E_FAIL);6.7.8.Gdiplus:Bitmap bmSrc(pStream);9.if(bmSrc.GetLastStatus()!=Gdiplus:Ok)10.11.return(E_FAIL);12.13.14.return(CreateFromGdiplusBitmap(bmSrc);15.16.17.inline HRESULTCImage:Load(LPCTSTR pszFileName)throw()18.19.if(!InitGDIPlus()20.21.return(E_FAIL);22.23.24.Gdiplus:Bitmap bmSrc(CT2W)pszFileName);25.if(bmSrc.GetLastStatus()!=Gdiplus:Ok)26.27.return(E_FAIL);28.29.30.return(CreateFromGdiplusBitmap(bmSrc);31.cppview plaincopy1.inline HRESULTCImage:Save(IStream*pStream,REFGUID guidFileType)const throw()2.3.if(!InitGDIPlus()4.5.return(E_FAIL);6.7.8.UINT nEncoders;9.UINT nBytes;10.Gdiplus:Status status;11.12.status=Gdiplus:GetImageEncodersSize(&nEncoders,&nBytes);13.if(status!=Gdiplus:Ok)14.15.return(E_FAIL);16.17.18.USES_ATL_SAFE_ALLOCA;19.Gdiplus:ImageCodecInfo*pEncoders=static_cast(_ATL_SAFE_ALLOCA(nBytes,_ATL_SAFE_ALLOCA_DEF_THRESHOLD);20.21.if(pEncoders=NULL)22.return E_OUTOFMEMORY;23.24.status=Gdiplus:GetImageEncoders(nEncoders,nBytes,pEncoders);25.if(status!=Gdiplus:Ok)26.27.return(E_FAIL);28.29.30.CLSID clsidEncoder=FindCodecForFileType(guidFileType,pEncoders,nEncoders);31.if(clsidEncoder=CLSID_NULL)32.33.return(E_FAIL);34.35.36.if(m_bHasAlphaChannel)37.38.ATLASSUME(m_nBPP=32);39.Gdiplus:Bitmap bm(m_nWidth,m_nHeight,m_nPitch,PixelFormat32bppARGB,static_cast(m_pBits);40.status=bm.Save(pStream,&clsidEncoder,NULL);41.if(status!=Gdiplus:Ok)42.43.return(E_FAIL);44.45.46.else47.48.Gdiplus:Bitmap bm(m_hBitmap,NULL);49.status=bm.Save(pStream,&clsidEncoder,NULL);50.if(status!=Gdiplus:Ok)51.52.return(E_FAIL);6.return(S_OK);57.58.59.inline HRESULTCImage:Save(LPCTSTR pszFileName,REFGUID guidFileType)const throw()60.61.if(!InitGDIPlus()62.63.return(E_FAIL);64.65.66.UINT nEncoders;67.UINT nBytes;68.Gdiplus:Status status;69.70.status=Gdiplus:GetImageEncodersSize(&nEncoders,&nBytes);71.if(status!=Gdiplus:Ok)72.73.return(E_FAIL);74.75.76.USES_CONVERSION_EX;77.Gdiplus:ImageCodecInfo*pEncoders=static_cast(_ATL_SAFE_ALLOCA(nBytes,_ATL_SAFE_ALLOCA_DEF_THRESHOLD);78.79.if(pEncoders=NULL)80.return E_OUTOFMEMORY;81.82.status=Gdiplus:GetImageEncoders(nEncoders,nBytes,pEncoders);83.if(status!=Gdiplus:Ok)84.85.return(E_FAIL);86.87.88.CLSID clsidEncoder=CLSID_NULL;89.if(guidFileType=GUID_NULL)90.91./Determine clsidfrom extension92.clsidEncoder=FindCodecForExtension(:PathFindExtension(pszFileName),pEncoders,nEncoders);93.94.else95.96./Determine clsidfrom filetype97.clsidEncoder=FindCodecForFileType(guidFileType,pEncoders,nEncoders);98.99.if(clsidEncoder=CLSID_NULL)100.101.return(E_FAIL);102.103.104.LPCWSTR pwszFileName=T2CW_EX(pszFileName,_ATL_SAFE_ALLOCA_DEF_THRESHOLD);105.#ifndef_UNICODE106.if(pwszFileName=NULL)107.return E_OUTOFMEMORY;108.#endif/_UNICODE109.if(m_bHasAlphaChannel)110.111.ATLASSUME(m_nBPP=32);112.Gdiplus:Bitmap bm(m_nWidth,m_nHeight,m_nPitch,PixelFormat32bppARGB,static_cast(m_pBits);113.status=bm.Save(pwszFileName,&clsidEncoder,NULL);114.if(status!=Gdiplus:Ok)115.116.return(E_FAIL);117.118.11
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 餐厨垃圾收集工岗前安全文化考核试卷含答案
- 硝酸铵结晶造粒工冲突管理知识考核试卷含答案
- 丁二酸装置操作工岗前安全强化考核试卷含答案
- 光刻工岗前安全实操考核试卷含答案
- 手风琴零件制作工安全检查考核试卷含答案
- 树桩盆景工安全知识强化考核试卷含答案
- 乙腈装置操作工冲突解决知识考核试卷含答案
- 白酒酵母工岗中工作实操考核试卷含答案
- 聚氨酯装置操作工基础管理测试考核试卷含答案
- 2026年黑龙江幼儿师范高等专科学校高职单招笔试综合素质历年参考题库含答案解析
- 2026新教材语文 2《啊我们踯躅于黑暗的丛林里!》教学课件
- 《机械制图》电子教材
- 2001年江苏省盐城市中考数学试题【含答案、解析】
- JG/T 346-2011合成树脂装饰瓦
- 老年公寓各项管理制度大全与创新案例
- 《建筑机器人的应用》课件
- 建设工程消防设计审查验收疑难问题解答(2024版)
- 《马克思主义发展史》题集
- 信用卡还款协议书
- 国家职业技术技能标准 6-29-01-07 乡村建设工匠 2024年版
- 机械加工工序卡
评论
0/150
提交评论