1.GDIPlus需要下载sdk开发包
2.在tool,option,direction,include files下设置头文件路径.../MICROSOFT SDK/INCLUDE,在tool,option,direction,libaray files下设置lib路径.../MICROSOFT SDK/SDK8/LIB,在project, setting, link, object/modules libaray里面输入gdiplus.lib.
注意,路径一定要放在最上层,还有,一定要是绝对路径,上面的...需要自己补充完整
3.新建一个单文档工程XXX,给XXXApp类添加成员变量
protected:
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
在XXXApp::InitInstance()里面添加:GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
重载XXXApp::ExitInstance(),在里面添加:GdiplusShutdown(m_gdiplusToken);
注意,以上修改过的文件前面要加上以下语句:
#include <GdiPlus.h>
using namespace Gdiplus;
上面的事情做完,你就可以在程序中使用gdiplus了,以下是一个把bmp转换成jpg的小例子:
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
void BMP2JPG(CString strBMPFile,CString strJPGFile)
{
CLSID encoderClsid;
WCHAR strGuid[39];
if(GetEncoderClsid(L"image/jpeg", &encoderClsid) >= 0)
StringFromGUID2(encoderClsid, strGuid, 39);
Image image(strBMPFile.AllocSysString());
image.Save(strJPGFile.AllocSysString(),&encoderClsid,NULL);
}
当然,上面的例子所在文件前面也必须加上
#include <GdiPlus.h>
using namespace Gdiplus;