首先得初始化GDI+
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_pGdiToken;
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
bool GetEncoderClsid(const WCHAR* format,CLSID *pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num,&size);
if (size == 0)
{
return false;
}
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
{
return false;
}
GetImageEncoders(num,size,pImageCodecInfo);
for (UINT i = 0;i < num;i++)
{
if (wcscmp(pImageCodecInfo[i].MimeType,format) == 0)
{
*pClsid = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return true;
}
}
free(pImageCodecInfo);
return false;
}
void BMP2JPG(CString strBMPFile,CString strJPGFile)
{
CLSID encoderClsid;
WCHAR strGuid[39];
if (GetEncoderClsid(L"image/jpeg",&encoderClsid) )
{
StringFromGUID2(encoderClsid,strGuid,39);
}
Image image(strBMPFile.AllocSysString());
image.Save(strJPGFile.AllocSysString(),&encoderClsid,NULL);
}