GDI+ 摘要: 保存图像文件
2024-10-16 05:44:19
要保存图像文件,必须先获得图像的编码格式信息。可是GDI+没有直接提供这个函数:GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
因此须要我们自己写一个 GetEncoderClsid 取得图像编码格式的函数
幸好,有 GetImageDecoders函数作为參照
- #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()
- 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:
- 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:
- 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;
- */
- }
最新文章
- Java程序员必须熟知的十项技术
- javascript练习-私有状态
- 爱上MVC系列~带扩展名的路由失效问题
- Intellij Idea 14 生成serialVersionUID的方法
- jq 截取字符串
- Eclipse快捷键列表大全
- python 返回函数
- iOS提交AppStore被拒原因
- Android杂谈--网络状态判断
- VS中新建网站和新建项目web应用程序的区别?(实际应用总结一点)
- HDU 3586 : Information Disturbing
- SharePoint2010添加webpart找不到内容编辑器
- Unity Notes调制粒子系统的颗粒的最大数目
- 算法---高速分拣(quick sort)
- 如何在Android Studio中指定NDK位置?
- php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0
- BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库
- 编码知识梳理(UTF-8, Unicode, GBK, X509, ANSI, VIM中编码)
- (转)在JAVA实现DataTable对象(三)——DataTable对象实现
- 安装和配置Apache服务器(下)
热门文章
- ssh连接失败,排错经验(转)
- 走向DBA[MSSQL篇] 详解游标
- .Net反编译实战
- 每天收获一点点------Hadoop之初始MapReduce
- 【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型
- 打造简易可扩展的jQuery/CSS3 Tab菜单
- 源码安装saltstack的时候遇到的问题
- Net开源网络爬虫
- jquery 调用wcf 的SOA架构,将三层架构运用到SOA的架构中来(第四天)
- Hadoop2.2.0--Hadoop Federation、Automatic HA、Yarn完全分布式集群结构