//用GDI+转换图形格式
#include "afxdtctl.h"
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#endif
INT GetEncoderClsid(const WCHAR* format, CLSID* pClsid); // helper function
INT main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
Status stat;
//Image* image = new Image(L"C:\\Test.jpg"); //这句在 MFC下报错
Image* image = NULL;
image = Bitmap::FromFile(L"C:\\Test.jpg", TRUE);
// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/bmp", &encoderClsid);
stat = image->Save(L"C:\\Test.bmp", &encoderClsid, NULL);
if(stat == Ok)
printf("Bird.png was saved successfully\n");
else
printf("Failure: stat = %d\n", stat);
delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}
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
}
//另
image/bmp
image/jpeg
image/gif
image/tiff
image/png
需要下载相应的GDI+库等,建议将VC SDK升级到最新
升级地址:
http://www.microsoft.com/msdownload/platformsdk/sdkupdate/
本文地址:http://com.8s8s.com/it/it25364.htm