Converting a BMP Image to a PNG Image

本文来自msdn,方便以后查询!当然闲来无事,看看是有益处的......

 

To save an image to a disk file, call the Save method of the Image class. The following console application loads a BMP image from a disk file, converts the image to the PNG format, and saves the converted image to a new disk file. The main function relies on the helper function GetEncoderClsid, which is shown in Retrieving the Class Identifier for an Encoder.

#include <windows.h> #include <gdiplus.h> #include <stdio.h> using namespace Gdiplus; 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"Bird.bmp"); // Get the CLSID of the PNG encoder. GetEncoderClsid(L"image/png", &encoderClsid); stat = image->Save(L"Bird.png", &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; } 

另外,检索编码器clsid的函数GetEncoderClsid在msdn的链接 Retrieving the Class Identifier for an Encoder为:

http://msdn.microsoft.com/en-us/library/ms533843(VS.85).aspx

 

分析以上代码可知,GetEncoderClsid(L"image/png", &encoderClsid)根据MIME类型“image/png”得到png格式编码器的clsid,这样在调用save方法保存image时,就会按照clsid指定的png编码格式将image编码为Bird.png文件保存在硬盘。因此编码器的clsid(encodeClsid)在这里起着很重要的作用,sava为何种格式完全依靠它!!!

 

你可能感兴趣的:(image,function,application,Class,disk,GDI+)