2.在你将要使用GDI+的工程中,完成初始化工作:
在StdAfx.h中加入
#define ULONG_PTR unsigned long
#include <Gdiplus.h>
using namespace Gdiplus;
3.在CXApp头文件中加入
ULONG_PTR m_gdiplusToken;
4.在 BOOL CXApp::InitInstance() 中添加
GdiplusStartupInput m_gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
5.在int CXApp::ExitInstance()加入(这个方法需要自己加入 在ClassWizard 中,注意在ClassName中选择CXApp,Messages列表框中选择ExitInstance)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
6.在Project->stting->Link->Intput->Additional Dependencies中加入gdiplus.lib
此时GDI+设置成功。
具体代码如下:
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 SaveFile(Bitmap* pImage, const wchar_t* pFileName)
{
EncoderParameters encoderParameters;
CLSID jpgClsid;
GetEncoderClsid(L"image/jpeg", &jpgClsid);
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
// Save the image as a JPEG with quality level 100.
ULONG quality;
quality = 100;
encoderParameters.Parameter[0].Value = &quality;
Status status = pImage->Save(pFileName, &jpgClsid, &encoderParameters);
if (status != Ok)
{
wprintf(L"%d Attempt to save %s failed./n", status, pFileName);
}
}
//调用方式:
Bitmap newbitmap(L"d://d.bmp");//加载BMP
const wchar_t *pFileName=L"d://new.jpg";//保存路径
SaveFile(&newbitmap,pFileName );
本文转自:http://blog.csdn.net/highyyy/article/details/6125763
posted on 2014-01-09 14:30
王海光 阅读(1383)
评论(0) 编辑 收藏 引用 所属分类:
MFC