如何:列出已安装的解码器

更新:2007 年 11 月

可能需要列出计算机可用的图像解码器,以确定应用程序是否可以读取某种特定的图像文件格式。 ImageCodecInfo 类提供了 GetImageDecoders 静态方法,以便您可以确定有哪些图像解码器可供使用。GetImageDecoders 返回 ImageCodecInfo 对象数组。

示例

下面的代码示例输出已安装的解码器及其属性值的列表。

Private Sub GetImageDecodersExample(ByVal e As PaintEventArgs)
    ' Get an array of available decoders.
    Dim myCodecs() As ImageCodecInfo
    myCodecs = ImageCodecInfo.GetImageDecoders()
    Dim numCodecs As Integer = myCodecs.GetLength(0)

    ' Set up display variables.
    Dim foreColor As Color = Color.Black
    Dim font As New Font("Arial", 8)
    Dim i As Integer = 0

    ' Check to determine whether any codecs were found.
    If numCodecs > 0 Then
        ' Set up an array to hold codec information. There are 9
        ' information elements plus 1 space for each codec, so 10 times
        ' the number of codecs found is allocated.
        Dim myCodecInfo(numCodecs * 10) As String

        ' Write all the codec information to the array.
        For i = 0 To numCodecs - 1
            myCodecInfo((i * 10)) = "Codec Name = " + myCodecs(i).CodecName
            myCodecInfo((i * 10 + 1)) = "Class ID = " + myCodecs(i).Clsid.ToString()
            myCodecInfo((i * 10 + 2)) = "DLL Name = " + myCodecs(i).DllName
            myCodecInfo((i * 10 + 3)) = "Filename Ext. = " + myCodecs(i).FilenameExtension
            myCodecInfo((i * 10 + 4)) = "Flags = " + myCodecs(i).Flags.ToString()
            myCodecInfo((i * 10 + 5)) = "Format Descrip. = " + myCodecs(i).FormatDescription
            myCodecInfo((i * 10 + 6)) = "Format ID = " + myCodecs(i).FormatID.ToString()
            myCodecInfo((i * 10 + 7)) = "MimeType = " + myCodecs(i).MimeType
            myCodecInfo((i * 10 + 8)) = "Version = " + myCodecs(i).Version.ToString()
            myCodecInfo((i * 10 + 9)) = " "
        Next i
        Dim numMyCodecInfo As Integer = myCodecInfo.GetLength(0)

        ' Render all of the information to the screen.
        Dim j As Integer = 20
        For i = 0 To numMyCodecInfo - 1
            e.Graphics.DrawString(myCodecInfo(i), _
                font, New SolidBrush(foreColor), 20, j)
            j += 12
        Next i
    Else
        e.Graphics.DrawString("No Codecs Found", _
            font, New SolidBrush(foreColor), 20, 20)
    End If
End Sub
private void GetImageDecodersExample(PaintEventArgs e)
{
    // Get an array of available decoders.
    ImageCodecInfo[] myCodecs;
    myCodecs = ImageCodecInfo.GetImageDecoders();
    int numCodecs = myCodecs.GetLength(0);

    // Set up display variables.
    Color foreColor = Color.Black;
    Font font = new Font("Arial", 8);
    int i = 0;

    // Check to determine whether any codecs were found.
    if (numCodecs > 0)
    {
        // Set up an array to hold codec information. There are 9
        // information elements plus 1 space for each codec, so 10 times
        // the number of codecs found is allocated.
        string[] myCodecInfo = new string[numCodecs * 10];

        // Write all the codec information to the array.
        for (i = 0; i < numCodecs; i++)
        {
            myCodecInfo[i * 10] = "Codec Name = " + myCodecs[i].CodecName;
            myCodecInfo[(i * 10) + 1] = "Class ID = " +
                myCodecs[i].Clsid.ToString();
            myCodecInfo[(i * 10) + 2] = "DLL Name = " + myCodecs[i].DllName;
            myCodecInfo[(i * 10) + 3] = "Filename Ext. = " +
                myCodecs[i].FilenameExtension;
            myCodecInfo[(i * 10) + 4] = "Flags = " +
                myCodecs[i].Flags.ToString();
            myCodecInfo[(i * 10) + 5] = "Format Descrip. = " +
                myCodecs[i].FormatDescription;
            myCodecInfo[(i * 10) + 6] = "Format ID = " +
                myCodecs[i].FormatID.ToString();
            myCodecInfo[(i * 10) + 7] = "MimeType = " + myCodecs[i].MimeType;
            myCodecInfo[(i * 10) + 8] = "Version = " +
                myCodecs[i].Version.ToString();
            myCodecInfo[(i * 10) + 9] = " ";
        }
        int numMyCodecInfo = myCodecInfo.GetLength(0);

        // Render all of the information to the screen.
        int j = 20;
        for (i = 0; i < numMyCodecInfo; i++)
        {
            e.Graphics.DrawString(myCodecInfo[i],
                font,
                new SolidBrush(foreColor),
                20,
                j);
            j += 12;
        }
    }
    else
        e.Graphics.DrawString("No Codecs Found",
            font,
            new SolidBrush(foreColor),
            20,
            20);
}

编译代码

此示例需要:

请参见

任务

如何:列出已安装的解码器

其他资源

在托管 GDI+ 中使用图像编码器和解码器