利用可能なImageCodecInfo (イメージコーデック) を列挙する
利用可能なイメージコーデックの情報を列挙するコードを紹介します。
概要
.NET Frameworkでは画像形式を変換したり、他の画像形式のファイルを読み込む用途でImageCodec(イメージコーデック)を利用できます。どのような画像形式で読み込めるか、保存できるかを確認するためにコーデックの一覧を取得したいことがあります。この記事ではPCにインストールされているイメージコーデックの一覧を取得するコードを紹介します。
コマンド
コーデックの一覧は
ImageCodecInfo.GetImageEncoders();
により取得できます。コーデックの一覧であるImageCodecInfoの配列を戻り値として返します。
サンプルコード
UI
以下のUIを作成します。MultiLineプロパティをtrueにしたTextBoxとButtonを配置します。
コード
下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace ImageEncoderList
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ImageCodecInfo[] myCodecs;
myCodecs = ImageCodecInfo.GetImageEncoders();
int numCodecs = myCodecs.GetLength(0);
if (numCodecs > 0) {
for (int i = 0; i < numCodecs; i++) {
textBox1.Text += string.Format("Codec Name = {0:s}\r\n", myCodecs[i].CodecName);
textBox1.Text += string.Format("Class ID = {0:s}\r\n", myCodecs[i].Clsid.ToString());
textBox1.Text += string.Format("DLL Name = {0:s}\r\n", myCodecs[i].DllName);
textBox1.Text += string.Format("Filename Ext. = {0:s}\r\n", myCodecs[i].FilenameExtension);
textBox1.Text += string.Format("Flags = {0:s}\r\n", myCodecs[i].Flags.ToString());
textBox1.Text += string.Format("Format Descrip. = {0:s}\r\n", myCodecs[i].FormatDescription);
textBox1.Text += string.Format("Format ID = {0:s}\r\n", myCodecs[i].FormatID.ToString());
textBox1.Text += string.Format("MimeType = {0:s}\r\n", myCodecs[i].MimeType);
textBox1.Text += string.Format("Version = {0:s}\r\n", myCodecs[i].Version.ToString());
textBox1.Text += "\r\n";
}
}
else {
textBox1.Text = "イメージコーデックはありませんでした。\r\n";
}
}
}
}
解説
myCodecs = ImageCodecInfo.GetImageEncoders();
int numCodecs = myCodecs.GetLength(0);
上記コードにより、イメージコーデックの一覧と個数を取得します。(個数は取得したイメージコーデックの配列の長さから求めます)
for (int i = 0; i < numCodecs; i++) {
textBox1.Text += string.Format("Codec Name = {0:s}\r\n", myCodecs[i].CodecName);
textBox1.Text += string.Format("Class ID = {0:s}\r\n", myCodecs[i].Clsid.ToString());
textBox1.Text += string.Format("DLL Name = {0:s}\r\n", myCodecs[i].DllName);
textBox1.Text += string.Format("Filename Ext. = {0:s}\r\n", myCodecs[i].FilenameExtension);
textBox1.Text += string.Format("Flags = {0:s}\r\n", myCodecs[i].Flags.ToString());
textBox1.Text += string.Format("Format Descrip. = {0:s}\r\n", myCodecs[i].FormatDescription);
textBox1.Text += string.Format("Format ID = {0:s}\r\n", myCodecs[i].FormatID.ToString());
textBox1.Text += string.Format("MimeType = {0:s}\r\n", myCodecs[i].MimeType);
textBox1.Text += string.Format("Version = {0:s}\r\n", myCodecs[i].Version.ToString());
textBox1.Text += "\r\n";
}
myCodecs配列の内容をテキストボックスに出力します。
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
[List]ボタンをクリックします。コーデックの情報がテキストボックスに表示されます。
表示結果
Codec Name = Built-in BMP Codec
Class ID = 557cf400-1a04-11d3-9a73-0000f81ef32e
DLL Name =
Filename Ext. = *.BMP;*.DIB;*.RLE
Flags = Encoder, Decoder, SupportBitmap, Builtin
Format Descrip. = BMP
Format ID = b96b3cab-0728-11d3-9d7b-0000f81ef32e
MimeType = image/bmp
Version = 1
Codec Name = Built-in JPEG Codec
Class ID = 557cf401-1a04-11d3-9a73-0000f81ef32e
DLL Name =
Filename Ext. = *.JPG;*.JPEG;*.JPE;*.JFIF
Flags = Encoder, Decoder, SupportBitmap, Builtin
Format Descrip. = JPEG
Format ID = b96b3cae-0728-11d3-9d7b-0000f81ef32e
MimeType = image/jpeg
Version = 1
Codec Name = Built-in GIF Codec
Class ID = 557cf402-1a04-11d3-9a73-0000f81ef32e
DLL Name =
Filename Ext. = *.GIF
Flags = Encoder, Decoder, SupportBitmap, Builtin
Format Descrip. = GIF
Format ID = b96b3cb0-0728-11d3-9d7b-0000f81ef32e
MimeType = image/gif
Version = 1
Codec Name = Built-in TIFF Codec
Class ID = 557cf405-1a04-11d3-9a73-0000f81ef32e
DLL Name =
Filename Ext. = *.TIF;*.TIFF
Flags = Encoder, Decoder, SupportBitmap, Builtin
Format Descrip. = TIFF
Format ID = b96b3cb1-0728-11d3-9d7b-0000f81ef32e
MimeType = image/tiff
Version = 1
Codec Name = Built-in PNG Codec
Class ID = 557cf406-1a04-11d3-9a73-0000f81ef32e
DLL Name =
Filename Ext. = *.PNG
Flags = Encoder, Decoder, SupportBitmap, Builtin
Format Descrip. = PNG
Format ID = b96b3caf-0728-11d3-9d7b-0000f81ef32e
MimeType = image/png
Version = 1
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用