封装了一个GDI+的使用类
GdiPluss.h
#pragma once#include#include using namespace Gdiplus;#pragma comment(lib, "gdiplus.lib")class CGdiPluss{public: CGdiPluss(void); ~CGdiPluss(void);public: enum ImageFormat { //Jpeg图片格式 Jpeg, //Png图片格式 Png, //Bmp图片格式 Bmp, //Gif图片格式 Gif, //Tiff图片格式 Tiff } ;public: //打开GDI+ void OpenGdiPlus(); //关闭GDI+ void CloseGdiPlus(); //获取Image的格式信息CLSID //参数:format,image的图片格式(image/png | image/bmp | image/jpeg | image/gif | image/tiff) //参数:CLSID,保存格式的信息 bool GetEncoderClsid(const WCHAR* format, CLSID* pClsid); //图片保存 //参数:src,保存的图片 //参数:filename,保存的路径 //参数:format,图片保存的格式 bool SaveImage(Bitmap* src,const WCHAR* filename, ImageFormat format);private: ULONG_PTR m_gdiplus_token;};
GdiPluss.cpp
#include "GdiPluss.h"CGdiPluss::CGdiPluss(void){}CGdiPluss::~CGdiPluss(void){}void CGdiPluss::OpenGdiPlus(){ ::GdiplusStartupInput startinput; ::GdiplusStartup(&m_gdiplus_token,&startinput,NULL);}void CGdiPluss::CloseGdiPlus(){ ::GdiplusShutdown(m_gdiplus_token);}bool CGdiPluss::GetEncoderClsid(const WCHAR* format, CLSID* pClsid){ UINT num, size; //获取GDI+支持的图像格式编码器种类数以及ImageCodecInfo数组的存放大小 Gdiplus::GetImageEncodersSize(&num, &size); if(size == 0) return false; // Failure //为ImageCodecInfo数组分配足额空间 Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size)); //获取所有的图像编码器信息 Gdiplus::GetImageEncoders(num, size, pImageCodecInfo); bool flag = false; //查找符合的图像编码器的Clsid for (UINT ix = 0; !flag && ix < num; ++ix) { //忽略大小写进行字符串比较 if ( _wcsicmp(pImageCodecInfo[ix].MimeType, format) == 0) { *pClsid = pImageCodecInfo[ix].Clsid; flag = true; } } free(pImageCodecInfo); return flag;}bool CGdiPluss::SaveImage(Bitmap* src,const WCHAR* filename, ImageFormat format){ CLSID clsid; WCHAR* _format; switch(format) { case Jpeg: _format=L"image/jpeg"; break; case Png: _format=L"image/png"; break; case Bmp: _format=L"image/bmp"; break; case Gif: _format=L"image/gif"; break; case Tiff: _format=L"image/tiff"; break; default: return false; break; } if(!GetEncoderClsid(_format,&clsid)) return false; src->Save(filename,&clsid); return true;}
使用很简单:
::CGdiPluss m_gdi;
开启
m_gdi.OpenGdiPlus();
关闭
m_gdi.CloseGdiPlus();