zoukankan      html  css  js  c++  java
  • GDI+ 摘要: 保存图像文件

    要保存图像文件,必须先获得图像的编码格式信息。可是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.   
    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 ", 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()

    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:

    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:

    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

  • 相关阅读:
    “猫癣”集团借IE7新漏洞再掀风浪 狼人:
    研究人员在黑帽安全大会演示SSL攻击 狼人:
    猫癣病毒“躲猫猫” 移师广东东莞月入百万 狼人:
    Adobe两款软件存在缺陷 黑客可控制用户PC 狼人:
    安全观点:遭遇数据泄露破坏 损失的不只是金钱 狼人:
    McAfee报告称七成手机制造商认为手机安全至关重要 狼人:
    微软表示本月将发布五个Windows 7更新 狼人:
    Gmail电子邮件曝全球性故障 谷歌向用户道歉 狼人:
    Google Talk被黑客利用 发动钓鱼攻击 狼人:
    谷歌GMail邮件服务出现故障 部分服务已恢复 狼人:
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4638895.html
Copyright © 2011-2022 走看看