摘自MSDN,其中 GetEncoderClsid 第一个参数可为
L"image/bmp" L"image/jpeg" L"image/gif" L"image/tiff" L"image/png"
#include <windows.h> #include <gdiplus.h> #include <stdio.h> using namespace Gdiplus; int GetEncoderClsid(const WCHAR* format, CLSID* pClsid); int main() { // Initialize GDI+. GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); CLSID encoderClsid; int result; WCHAR strGuid[39]; result = GetEncoderClsid(L"image/png", &encoderClsid); if(result < 0) { printf("The PNG encoder is not installed.\n"); } else { Bitmap bmp(L"logo.bmp"); bmp.Save(L"logo.png", &encoderClsid, NULL); } 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 }