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:
仿照上例 ,我们编写自己的,获得编码格式的函数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;
*/
}
要将图像文件保存,必须先获得图像的编码格式信息,但是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;
*/
}
相关文章推荐
- GDI+ 总结一: 保存图像文件
- GDI+ 总结一: 保存图像文件
- GDI+ 将BMP 32图像转换为JPG文件格式保存
- 总结:C# Bitmap保存的问题,eg:GDI+中发生一般性错误 .jpg 正由另一个进程使用 ,该进程无法访问文件
- GDI+将CDC中画好的图像,保存到文件中
- GDI+ 摘要: 保存图像文件
- MFC中将所画图像保存到图像文件
- 处理2D图像和纹理——创建一张纹理,定义每个像素的颜色,将纹理保存到一个文件
- XML保存图像(其他?)文件
- 大哥大姐,帮小弟一个忙啊。。。关于二进制文件保存为jpeg图像。。。急啊。。。
- 浅谈BMP图像文件的内部结构(长期研究的总结)
- mfc/c++ 保存clipBoard剪切板中的图像到文件,以及从文件读出BMP文件的指定像素
- 在RFT中如何截取屏幕图像并保存到文件中?
- 关于PhotoShop中保存CMYK格式TIFF文件在GDI+错误地显示颜色的问题解决方法
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 【ASP.NET】使用Stream方式上传文件(不用FileUpload),保存图像的缩略图
- 将CBitmap类中的图像保存到文件
- .net读取图像并保存为文件
- 保存HBITMAP图像到文件中