Share via


メタデータの読み取り

一部のイメージ ファイルにはメタデータが含まれており、このデータを読み取って、そのイメージの特徴を確認できます。デジタル写真にはこのようなメタデータが含まれていることがあり、そのメタデータを読み取って、たとえばイメージを取り込むために使用したカメラのメーカーやモデルを確認できます。GDI+ では、既存のメタデータを読み取ったり、新しいメタデータをイメージ ファイルに書き込んだりできます。

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

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

Id

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

16 進値 説明
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

イメージのタイトル

機器のメーカー

機器のモデル

ExifDTOriginal

Exif 露光時間

輝度テーブル

クロミナンス テーブル

Value

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

Len

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

Type

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

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

ファイル FakePhoto.jpg からメタデータの 7 つの部分を読み取って表示する例を次に示します。

'Create an Image object. 
Dim image = New Bitmap("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
[C#]
//Create an Image object. 
Image image = new Bitmap("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++;
}

上のコードは、次のような出力を生成します。

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

リスト内の 2 番目の (インデックス 1) プロパティ項目には、Id 0x010F (機器のメーカー) と Type 2 (ASCII 形式でエンコードされたバイト配列) が指定されています。次のコードは、上の例から続いており、そのプロパティ項目の値を表示します。

'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)
[C#]
//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);

上のコードは、次のような出力を生成します。

The equipment make is Northwind Camera.