次の方法で共有


方法: イメージ メタデータを読み取る

一部のイメージ ファイルには、画像の特徴を判断するために読み取ることができるメタデータが含まれています。 たとえば、デジタル写真には、画像のキャプチャに使用されるカメラの作成とモデルを決定するために読み取ることができるメタデータが含まれている場合があります。 GDI+ を使用すると、既存のメタデータを読み取ることができ、イメージ ファイルに新しいメタデータを書き込むこともできます。

GDI+ は、個々のメタデータを PropertyItem オブジェクトに格納します。 PropertyItems オブジェクトのImage プロパティを読み取り、ファイルからすべてのメタデータを取得できます。 PropertyItems プロパティは、PropertyItem オブジェクトの配列を返します。

PropertyItem オブジェクトには、IdValueLenTypeの 4 つのプロパティがあります。

ID (アイディー)

メタデータ項目を識別するタグ。 Idに割り当てることができるいくつかの値を次の表に示します。

16 進数値 説明
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
画像のタイトル

機器メーカー

機器モデル

ExifDTOriginal

Exif 露出時間

輝度テーブル

Chrominance テーブル

価値

値の配列。 値の形式は、 Type プロパティによって決まります。

レン

Value プロパティが指す値の配列の長さ (バイト単位)。

タイプ

Value プロパティが指す配列内の値のデータ型。 Type プロパティ値で示される形式を次の表に示します。

数値 説明
1 Byte
2 ASCII としてエンコードされた Byte オブジェクトの配列
3 16 ビット整数
4 32 ビット整数
5 有理数を表す 2 つの Byte オブジェクトの配列
6 未使用
7 未定義
8 未使用
9 SLong
10 SRational

次のコード例では、ファイル FakePhoto.jpg内の 7 つのメタデータを読み取って表示します。 リスト内の 2 番目の (インデックス 1) プロパティ項目には、 Id 0x010F (機器の製造元) と Type 2 (ASCII エンコードバイト配列) があります。 このコード例では、そのプロパティ項目の値を表示します。

// Create an Image object.
Image image = new Bitmap(@"c:\FakePhoto.jpg");

// Get the PropertyItems property from image.
PropertyItem[] propItems = image.PropertyItems;

// Set up the display.
Font font = new Font("Arial", 12);
SolidBrush blackBrush = new SolidBrush(Color.Black);
int X = 0;
int Y = 0;

// For each PropertyItem in the array, display the ID, type, and
// length.
int count = 0;
foreach (PropertyItem propItem in propItems)
{
    e.Graphics.DrawString(
    "Property Item " + count.ToString(),
    font,
    blackBrush,
    X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   id: 0x" + propItem.Id.ToString("x"),
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   type: " + propItem.Type.ToString(),
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    e.Graphics.DrawString(
       "   length: " + propItem.Len.ToString() + " bytes",
       font,
       blackBrush,
       X, Y);

    Y += font.Height;

    count++;
}
// Convert the value of the second property to a string, and display
// it.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string manufacturer = encoding.GetString(propItems[1].Value);

e.Graphics.DrawString(
   "The equipment make is " + manufacturer + ".",
   font,
   blackBrush,
   X, Y);
'Create an Image object. 
Dim image As Bitmap = New Bitmap("c:\FakePhoto.jpg")

'Get the PropertyItems property from image.
Dim propItems As PropertyItem() = image.PropertyItems

'Set up the display.
Dim font As New Font("Arial", 12)
Dim blackBrush As New SolidBrush(Color.Black)
Dim X As Integer = 0
Dim Y As Integer = 0

'For each PropertyItem in the array, display the ID, type, and length.
Dim count As Integer = 0
Dim propItem As PropertyItem
For Each propItem In propItems
    e.Graphics.DrawString( _
       "Property Item " & count.ToString(), _
       font, _
       blackBrush, _
       X, Y)

    Y += font.Height

    e.Graphics.DrawString( _
       "   id: 0x" & propItem.Id.ToString("x"), _
       font, _
       blackBrush, _
       X, Y)

    Y += font.Height

    e.Graphics.DrawString( _
       "   type: " & propItem.Type.ToString(), _
       font, _
       blackBrush, _
       X, Y)

    Y += font.Height

    e.Graphics.DrawString( _
       "   length: " & propItem.Len.ToString() & " bytes", _
       font, _
       blackBrush, _
       X, Y)

    Y += font.Height

    count += 1
Next propItem
'Convert the value of the second property to a string, and display it.
Dim encoding As New System.Text.ASCIIEncoding()
Dim manufacturer As String = encoding.GetString(propItems(1).Value)

e.Graphics.DrawString( _
   "The equipment make is " & manufacturer & ".", _
   font, _
   blackBrush, _
   X, Y)

このコードでは、次のような出力が生成されます。

Property Item 0

id: 0x320

type: 2

length: 16 bytes

Property Item 1

id: 0x10f

type: 2

length: 17 bytes

Property Item 2

id: 0x110

type: 2

length: 7 bytes

Property Item 3

id: 0x9003

type: 2

length: 20 bytes

Property Item 4

id: 0x829a

type: 5

length: 8 bytes

Property Item 5

id: 0x5090

type: 3

length: 128 bytes

Property Item 6

id: 0x5091

type: 3

length: 128 bytes

The equipment make is Northwind Camera.

コードのコンパイル

前の例は Windows フォームで使用できるように設計されており、PaintEventArgs イベント ハンドラーのパラメーターである ePaintが必要です。 フォームの Paint イベントを処理し、このコードをペイント イベント ハンドラーに貼り付けます。 FakePhoto.jpgをシステムで有効なイメージ名とパスに置き換え、System.Drawing.Imaging名前空間をインポートする必要があります。

こちらも参照ください