Dela via


Hämtar klassidentifieraren för en kodare

Funktionen GetEncoderClsid i följande exempel tar emot MIME-typen för en kodare och returnerar klassidentifieraren (CLSID) för kodaren. MIME-typerna för kodarna som är inbyggda i Windows GDI+ är följande:

  • bild/bmp
  • bild/jpeg
  • bild/gif
  • image/tiff
  • bild/png

Funktionen anropar GetImageEncoders för att hämta en matris med ImageCodecInfo objekt. Om ett av ImageCodecInfo- objekt i matrisen representerar den begärda kodaren returnerar funktionen indexet för objektet ImageCodecInfo och kopierar CLSID- till variabeln som pekas på av pClsid. Om funktionen misslyckas returnerar den –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
}

Följande konsolprogram anropar funktionen GetEncoderClsid för att fastställa CLSID- för PNG-kodaren:

#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;
}

När du kör föregående konsolprogram får du utdata som liknar följande:

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}.