//此代码可以在以下形式中BMP DIB RLE GIF JPEG EMF TIFF PNG 随意转换
#include <Stdio.h>
#include <Objbase.h>
#include <Windows.h>
#include <Gdiplus.h>
#include <GdiPlusEnums.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus") // 文件需要gdiplus头文件
// Helper functions
int GetCodecClsid(const WCHAR*, CLSID*);
int main()
{
CLSID codecClsid; //定义一个组件类
EncoderParameters encoderParameters; //.NET Framework 类库,封装 EncoderParameter 对象的数组。
long quality; //定义一个长整型
Status stat; //好像是一个数据结构
GdiplusStartupInput gdiplusStartupInput; //似乎是定义一个一个启动gdiplus的进入量
ULONG gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); //Gdiplus启动函数,要给一个输入
{
// Get an image from the disk.
Image image(L"E:\capture.bmp"); //调用image组件,取位于特定位置的bmp图像
// Get the CLSID of the JPEG codec.
GetCodecClsid(L"image/jpeg", &codecClsid); //调用了GetCodecClsid函数,此函数的功能是提供多功能转换,
//其中包括进行不同图像格式的转换
// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value (of type LONG)
// in the array. We will set this value to 0, 50, and 100.
encoderParameters.Count = 1; //译码器参数设为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 0.
quality = 0;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save(L"Shapes001.jpg", &codecClsid, &encoderParameters); //codec多媒体数字信号译码器
if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes001.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes001.jpg");
// Save the image as a JPEG with quality level 50.
quality = 30;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save(L"Shapes050.jpg", &codecClsid, &encoderParameters);
if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes050.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes030.jpg");
// Save the image as a JPEG with quality level 100.
quality = 1000;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save(L"Shapes100.jpg", &codecClsid, &encoderParameters);
if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes100.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes100.jpg");
}
GdiplusShutdown(gdiplusToken);
return 0;
} // main
int GetCodecClsid(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(int j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
return j; // Success
}
} // for
return -1; // Failure
} // GetCodecClsid
//download gdiplus.dll
//http://www.microsoft.com/downloads/release.asp?releaseid=32738
//Platform SDK Redistributable: GDI+ RTM