GDI+我們可以很方便的對bmp、jpeg、gif、tiff、png格式的圖片進行轉換

引言:通過GDI+我們可以很方便的對bmp、jpeg、gif、tiff、png格式的圖片進行轉換。

步驟:

1)    通過GdiplusStartup初始化GDI+,以便後續的GDI+函數可以成功調用。

2)    通過GetImageEncodersSize獲取GDI+支持的圖像格式編碼器種類數numEncoders以及ImageCodecInfo數組的存放大小size

3)    通過malloc爲ImageCodecInfo數組分配足額空間。

4)    通過GetImageDecoders獲取所有的圖像編碼器信息。

5)    查看ImageCodecInfo.MimeType,查找符合的圖像編碼器的Clsid。

6)    釋放步驟3)分配的內存。

7)    創建Image對象並加載圖片。

8)    調用Image.Save方法進行圖片格式轉換,並把步驟3)得到的圖像編碼器Clsid傳遞給它。

9)    釋放Image對象。

10)  通過GdiplusShutdown清理所有GDI+資源。

示例:

  1. #include <windows.h>   
  2. #include <gdiplus.h>   
  3. #include <stdio.h>   
  4. using namespace Gdiplus;  
  5.   
  6. #pragma comment(lib,"gdiplus")   
  7.   
  8. int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)  
  9. {  
  10.    UINT  num = 0;          // number of image encoders   
  11.    UINT  size = 0;         // size of the image encoder array in bytes   
  12.   
  13.    ImageCodecInfo* pImageCodecInfo = NULL;  
  14.      
  15.    //2.獲取GDI+支持的圖像格式編碼器種類數以及ImageCodecInfo數組的存放大小   
  16.    GetImageEncodersSize(&num, &size);  
  17.    if(size == 0)  
  18.       return -1;  // Failure   
  19.   
  20.    //3.爲ImageCodecInfo數組分配足額空間   
  21.    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));  
  22.    if(pImageCodecInfo == NULL)  
  23.       return -1;  // Failure   
  24.   
  25.    //4.獲取所有的圖像編碼器信息   
  26.    GetImageEncoders(num, size, pImageCodecInfo);  
  27.   
  28.    //5.查找符合的圖像編碼器的Clsid   
  29.    for(UINT j = 0; j < num; ++j)  
  30.    {  
  31.       if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )  
  32.       {  
  33.          *pClsid = pImageCodecInfo[j].Clsid;  
  34.          free(pImageCodecInfo);  
  35.          return j;  // Success   
  36.       }      
  37.    }  
  38.   
  39.    //6.釋放步驟3分配的內存   
  40.    free(pImageCodecInfo);  
  41.    return -1;  // Failure   
  42. }  
  43.   
  44. INT main()  
  45. {  
  46.    GdiplusStartupInput gdiplusStartupInput;  
  47.    ULONG_PTR gdiplusToken;  
  48.      
  49.    //1.初始化GDI+,以便後續的GDI+函數可以成功調用   
  50.    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);  
  51.   
  52.    CLSID   encoderClsid;  
  53.    Status  stat;  
  54.      
  55.    //7.創建Image對象並加載圖片   
  56.    Image*   image = new Image(L"f://11.bmp");  
  57.   
  58.    // Get the CLSID of the PNG encoder.   
  59.    GetEncoderClsid(L"image/png", &encoderClsid);  
  60.   
  61.    //8.調用Image.Save方法進行圖片格式轉換,並把步驟3)得到的圖像編碼器Clsid傳遞給它   
  62.    stat = image->Save(L"11.png", &encoderClsid, NULL);  
  63.   
  64.    if(stat == Ok)  
  65.       printf("Bird.png was saved successfully/n");  
  66.    else  
  67.       printf("Failure: stat = %d/n", stat);   
  68.   
  69.    //9.釋放Image對象   
  70.    delete image;  
  71.    //10.清理所有GDI+資源   
  72.    GdiplusShutdown(gdiplusToken);  
  73.    return 0;  
  74. }  

學習資源:

MSDN -> Win32  和 COM開發 -> Graghics and Multimedia  ->  GDI+ -> Using GDI+ ->  Using Image Encoders and Decoders。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章