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

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

幸好,有 GetImageDecoders函数作为參照

  1. #include <windows.h>
  2. #include <gdiplus.h>
  3. #include <stdio.h>
  4. using namespace Gdiplus;
  5. INT main()
  6. {
  7. // Initialize GDI+.
  8. GdiplusStartupInput gdiplusStartupInput;
  9. ULONG_PTR           gdiplusToken;
  10. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  11. UINT  num;        // number of image decoders
  12. UINT  size;       // size, in bytes, of the image decoder array
  13. ImageCodecInfo* pImageCodecInfo;
  14. // How many decoders are there?
  15. // How big (in bytes) is the array of all ImageCodecInfo objects?
  16. GetImageDecodersSize(&num, &size);
  17. // Create a buffer large enough to hold the array of ImageCodecInfo
  18. // objects that will be returned by GetImageDecoders.
  19. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  20. // GetImageDecoders creates an array of ImageCodecInfo objects
  21. // and copies that array into a previously allocated buffer.
  22. // The third argument, imageCodecInfos, is a pointer to that buffer.
  23. GetImageDecoders(num, size, pImageCodecInfo);
  24. // Display the graphics file format (MimeType)
  25. // for each ImageCodecInfo object.
  26. for(UINT j = 0; j < num; ++j)
  27. {
  28. wprintf(L"%s\n", pImageCodecInfo[j].MimeType);
  29. }
  30. free(pImageCodecInfo);
  31. GdiplusShutdown(gdiplusToken);
  32. return 0;
  33. }

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()

  1. INT GetEncoderClsid(const WCHAR *format, CLSID *pClsid)
  2. {
  3. UINT  num = 0;          // number of image encoders
  4. UINT  size = 0;         // size of the image encoder array in bytes
  5. ImageCodecInfo* pImageCodecInfo = NULL;
  6. GetImageEncodersSize(&num, &size);
  7. if(size == 0)
  8. return -1;  // Failure
  9. pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  10. if(pImageCodecInfo == NULL)
  11. return -1;  // Failure
  12. GetImageEncoders(num, size, pImageCodecInfo);
  13. for(UINT j = 0; j < num; ++j)
  14. {
  15. if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
  16. {
  17. *pClsid = pImageCodecInfo[j].Clsid;
  18. free(pImageCodecInfo);
  19. return j;  // Success
  20. }
  21. }
  22. free(pImageCodecInfo);
  23. return -1;  // Failure
  24. }

保存图像文件:

Example_1:

  1. VOID Example_SaveFile(HDC hdc)
  2. {
  3. Graphics graphics(hdc);
  4. // Create an Image object based on a PNG file.
  5. Image  image(L"Mosaic.png");
  6. // Draw the image.
  7. graphics.DrawImage(&image, 10, 10);
  8. // Construct a Graphics object based on the image.
  9. Graphics imageGraphics(&image);
  10. // Alter the image.
  11. SolidBrush brush(Color(255, 0, 0, 255));
  12. imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
  13. // Draw the altered image.
  14. graphics.DrawImage(&image, 200, 10);
  15. // Save the altered image.
  16. CLSID pngClsid;
  17. GetEncoderClsid(L"image/png", &pngClsid);
  18. image.Save(L"Mosaic2.png", &pngClsid, NULL);
  19. }

Example_2:

  1. void CMyView::SavePic(HBITMAP hBitmap, CString szPicFilePath)
  2. {
  3. if(!hBitmap) return;
  4. if(PathFileExists(szPicFilePath))
  5. CFile::Remove(szPicFilePath);
  6. BITMAP bm;
  7. GetObject(hBitmap,sizeof(BITMAP),&bm);
  8. WORD BitsPerPixel=bm.bmBitsPixel;
  9. using namespace Gdiplus;
  10. Bitmap* bitmap=Bitmap::FromHBITMAP(hBitmap,NULL);
  11. EncoderParameters encoderParameters;
  12. ULONG compression;
  13. CLSID clsid;
  14. if(BitsPerPixel==1)
  15. {
  16. compression=EncoderValueCompressionCCITT4;
  17. }
  18. else
  19. {
  20. compression=EncoderValueCompressionLZW;
  21. }
  22. GetEncoderClsid(L"image/tiff", &clsid);
  23. encoderParameters.Count=1;
  24. encoderParameters.Parameter[0].Guid=EncoderCompression;
  25. encoderParameters.Parameter[0].Type=EncoderParameterValueTypeLong;
  26. encoderParameters.Parameter[0].NumberOfValues=1;
  27. encoderParameters.Parameter[0].Value=&compression;
  28. bitmap->Save(szPicFilePath,&clsid,&encoderParameters);
  29. delete bitmap;
  30. /*
  31. compression=100;
  32. GetEncoderClsid(L"image/jpeg", &clsid);
  33. encoderParameters.Count = 1;
  34. encoderParameters.Parameter[0].Guid = EncoderQuality;
  35. encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
  36. encoderParameters.Parameter[0].NumberOfValues = 1;
  37. encoderParameters.Parameter[0].Value =&compression;
  38. */
  39. }

转会:http://blog.csdn.net/shuilan0066/article/details/7084742

最新文章

  1. Java程序员必须熟知的十项技术
  2. javascript练习-私有状态
  3. 爱上MVC系列~带扩展名的路由失效问题
  4. Intellij Idea 14 生成serialVersionUID的方法
  5. jq 截取字符串
  6. Eclipse快捷键列表大全
  7. python 返回函数
  8. iOS提交AppStore被拒原因
  9. Android杂谈--网络状态判断
  10. VS中新建网站和新建项目web应用程序的区别?(实际应用总结一点)
  11. HDU 3586 : Information Disturbing
  12. SharePoint2010添加webpart找不到内容编辑器
  13. Unity Notes调制粒子系统的颗粒的最大数目
  14. 算法---高速分拣(quick sort)
  15. 如何在Android Studio中指定NDK位置?
  16. php5.6,Ajax报错,Warning: Cannot modify header information - headers already sent in Unknown on line 0
  17. BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库
  18. 编码知识梳理(UTF-8, Unicode, GBK, X509, ANSI, VIM中编码)
  19. (转)在JAVA实现DataTable对象(三)——DataTable对象实现
  20. 安装和配置Apache服务器(下)

热门文章

  1. ssh连接失败,排错经验(转)
  2. 走向DBA[MSSQL篇] 详解游标
  3. .Net反编译实战
  4. 每天收获一点点------Hadoop之初始MapReduce
  5. 【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型
  6. 打造简易可扩展的jQuery/CSS3 Tab菜单
  7. 源码安装saltstack的时候遇到的问题
  8. Net开源网络爬虫
  9. jquery 调用wcf 的SOA架构,将三层架构运用到SOA的架构中来(第四天)
  10. Hadoop2.2.0--Hadoop Federation、Automatic HA、Yarn完全分布式集群结构