您的位置:首页 > 其它

GDI+ 总结一: 保存图像文件

2014-12-28 20:54 253 查看
原文:GDI+ 总结:保存图像文件

要将图像文件保存,必须先获得图像的编码格式信息,但是GDI+没有直接提供这个函数:GetEncoderClsid(const WCHAR* format, CLSID* pClsid)



因此需要我们自己写一个 GetEncoderClsid 取得图像编码格式的函数



幸好,有 GetImageDecoders函数作为参照

[cpp]
view plaincopy

#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;

INT main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

UINT num; // number of image decoders
UINT size; // size, in bytes, of the image decoder array

ImageCodecInfo* pImageCodecInfo;

// How many decoders are there?
// How big (in bytes) is the array of all ImageCodecInfo objects?
GetImageDecodersSize(&num, &size);

// Create a buffer large enough to hold the array of ImageCodecInfo
// objects that will be returned by GetImageDecoders.
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));

// GetImageDecoders creates an array of ImageCodecInfo objects
// and copies that array into a previously allocated buffer.
// The third argument, imageCodecInfos, is a pointer to that buffer.
GetImageDecoders(num, size, pImageCodecInfo);

// Display the graphics file format (MimeType)
// for each ImageCodecInfo object.
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()

[cpp]
view plaincopy

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
}



保存图像文件:



Example_1:

[cpp]
view plaincopy

VOID Example_SaveFile(HDC hdc)
{
Graphics graphics(hdc);

// Create an Image object based on a PNG file.
Image image(L"Mosaic.png");

// Draw the image.
graphics.DrawImage(&image, 10, 10);

// Construct a Graphics object based on the image.
Graphics imageGraphics(&image);

// Alter the image.
SolidBrush brush(Color(255, 0, 0, 255));
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);

// Draw the altered image.
graphics.DrawImage(&image, 200, 10);

// Save the altered image.
CLSID pngClsid;
GetEncoderClsid(L"image/png", &pngClsid);
image.Save(L"Mosaic2.png", &pngClsid, NULL);
}



Example_2:

[cpp]
view plaincopy

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;
/*
compression=100;
GetEncoderClsid(L"image/jpeg", &clsid);

encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;
encoderParameters.Parameter[0].Value =&compression;
*/
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: