要保存图像文件,必须先获得图像的编码格式信息。可是GDI+没有直接提供这个函数:GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
因此须要我们自己写一个 GetEncoderClsid 取得图像编码格式的函数
幸好,有 GetImageDecoders函数作为參照
-
#include <windows.h>
-
#include <gdiplus.h>
-
#include <stdio.h>
-
using namespace Gdiplus;
-
-
INT main()
-
{
-
-
GdiplusStartupInput gdiplusStartupInput;
-
ULONG_PTR gdiplusToken;
-
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
-
-
UINT num;
-
UINT size;
-
-
ImageCodecInfo* pImageCodecInfo;
-
-
-
-
GetImageDecodersSize(&num, &size);
-
-
-
-
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
-
-
-
-
-
GetImageDecoders(num, size, pImageCodecInfo);
-
-
-
-
for(UINT j = 0; j < num; ++j)
-
{
-
wprintf(L"%s\n", pImageCodecInfo[j].MimeType);
-
}
-
-
free(pImageCodecInfo);
-
GdiplusShutdown(gdiplusToken);
-
return 0;
-
}
The preceding code produces the following output:
image/bmp
image/jpeg
image/gif
image/x-emf
image/x-wmf
image/tiff
image/png
image/x-icon
仿照上例 ,我们编写自己的。获得编码格式的函数GetEncoderClsid()
-
INT GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
-
{
-
UINT num = 0;
-
UINT size = 0;
-
-
ImageCodecInfo* pImageCodecInfo = NULL;
-
-
GetImageEncodersSize(&num, &size);
-
if(size == 0)
-
return -1;
-
-
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
-
if(pImageCodecInfo == NULL)
-
return -1;
-
-
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;
-
}
-
}
-
-
free(pImageCodecInfo);
-
return -1;
-
}
保存图像文件:
Example_1:
-
VOID Example_SaveFile(HDC hdc)
-
{
-
Graphics graphics(hdc);
-
-
-
Image image(L"Mosaic.png");
-
-
-
graphics.DrawImage(&image, 10, 10);
-
-
-
Graphics imageGraphics(&image);
-
-
-
SolidBrush brush(Color(255, 0, 0, 255));
-
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
-
-
-
graphics.DrawImage(&image, 200, 10);
-
-
-
CLSID pngClsid;
-
GetEncoderClsid(L"image/png", &pngClsid);
-
image.Save(L"Mosaic2.png", &pngClsid, NULL);
-
}
Example_2:
-
void CMyView::SavePic(HBITMAP hBitmap, CString szPicFilePath)
-
{
-
if(!hBitmap) return;
-
-
if(PathFileExists(szPicFilePath))
-
CFile::Remove(szPicFilePath);
-
-
BITMAP bm;
-
GetObject(hBitmap,sizeof(BITMAP),&bm);
-
WORD BitsPerPixel=bm.bmBitsPixel;
-
-
using namespace Gdiplus;
-
Bitmap* bitmap=Bitmap::FromHBITMAP(hBitmap,NULL);
-
EncoderParameters encoderParameters;
-
ULONG compression;
-
CLSID clsid;
-
-
if(BitsPerPixel==1)
-
{
-
compression=EncoderValueCompressionCCITT4;
-
}
-
else
-
{
-
compression=EncoderValueCompressionLZW;
-
}
-
GetEncoderClsid(L"image/tiff", &clsid);
-
-
encoderParameters.Count=1;
-
encoderParameters.Parameter[0].Guid=EncoderCompression;
-
encoderParameters.Parameter[0].Type=EncoderParameterValueTypeLong;
-
encoderParameters.Parameter[0].NumberOfValues=1;
-
encoderParameters.Parameter[0].Value=&compression;
-
-
bitmap->Save(szPicFilePath,&clsid,&encoderParameters);
-
delete bitmap;
-
-
-
-
-
-
-
-
-
-
-
}
转会:http://blog.csdn.net/shuilan0066/article/details/7084742