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

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

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

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

Id

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

16 進数値 説明
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
画像タイトル

機器の製造元

機器のモデル

ExifDTOriginal

Exif 露出時間

輝度テーブル

クロミナンス テーブル

値の配列です。 値の形式は Type プロパティで決まります。

Len

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 名前空間をインポートする必要があります。

関連項目