次の方法で共有


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

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

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

PropertyItem オブジェクトには、Id、Value、Len、および Type という 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 形式でエンコードされたバイト配列) が指定されています。 このコード例は、このプロパティ項目の値を表示します。

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

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.

コード

        '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)

// 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);

コードのコンパイル

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

参照

その他の技術情報

イメージ、ビットマップ、およびメタファイル

イメージ、ビットマップ、アイコン、およびメタファイルの操作