GDI+ 摘要: 保存图像文件

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

 

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

 

幸好,有 GetImageDecoders函数作为參照

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

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 plain copy
  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.   
  6.     ImageCodecInfo* pImageCodecInfo = NULL;     
  7.   
  8.     GetImageEncodersSize(&num, &size);     
  9.     if(size == 0)     
  10.         return -1;  // Failure      
  11.   
  12.     pImageCodecInfo = (ImageCodecInfo*)(malloc(size));     
  13.     if(pImageCodecInfo == NULL)     
  14.         return -1;  // Failure      
  15.   
  16.     GetImageEncoders(num, size, pImageCodecInfo);     
  17.   
  18.     for(UINT j = 0; j < num; ++j)     
  19.     {     
  20.         if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )     
  21.         {     
  22.             *pClsid = pImageCodecInfo[j].Clsid;     
  23.             free(pImageCodecInfo);     
  24.             return j;  // Success      
  25.         }         
  26.     }     
  27.   
  28.     free(pImageCodecInfo);     
  29.     return -1;  // Failure     
  30. }    


 

保存图像文件:

 

Example_1:

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


 

Example_2:

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


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

转载于:https://www.cnblogs.com/mengfanrong/p/4638895.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值