擷取編碼器的類別識別碼
下列範例中的 GetEncoderClsid 函式會接收編碼器的 MIME 類型,並傳回該編碼器的 CLSID) (類別識別碼。 Windows GDI+ 內建編碼器的 MIME 類型如下所示:
- image/bmp
- image/jpeg
- image/gif
- image/tiff
- image/png
函式會呼叫 GetImageEncoders 以取得 ImageCodecInfo 物件的陣列。 如果該陣列中的其中一個 ImageCodecInfo 物件代表要求的編碼器,函式會傳回 ImageCodecInfo 物件的索引,並將 CLSID 複製到 pClsid所指向的變數中。 如果函式失敗,則會傳回 –1。
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
}
下列主控台應用程式會呼叫 GetEncoderClsid 函式,以判斷 PNG 編碼器的 CLSID :
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
#include "GdiplusHelperFunctions.h"
INT main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
INT result;
WCHAR strGuid[39];
result = GetEncoderClsid(L"image/png", &encoderClsid);
if(result < 0)
{
printf("The PNG encoder is not installed.\n");
}
else
{
StringFromGUID2(encoderClsid, strGuid, 39);
printf("An ImageCodecInfo object representing the PNG encoder\n");
printf("was found at position %d in the array.\n", result);
wprintf(L"The CLSID of the PNG encoder is %s.\n", strGuid);
}
GdiplusShutdown(gdiplusToken);
return 0;
}
當您執行上述主控台應用程式時,您會取得類似下列的輸出:
An ImageCodecInfo object representing the PNG encoder
was found at position 4 in the array.
The CLSID of the PNG encoder is {557CF406-1A04-11D3-9A73-0000F81EF32E}.