Freigeben über


Vorgehensweise: Lesen von Bildmetadaten

Einige Bilddateien enthalten Metadaten, die Sie lesen können, um Features des Bilds zu bestimmen. Beispielsweise kann ein digitales Foto Metadaten enthalten, die Sie lesen können, um die Herstellung und das Modell der Kamera zu bestimmen, die zum Aufnehmen des Bilds verwendet wird. Mit GDI+ können Sie vorhandene Metadaten lesen und auch neue Metadaten in Bilddateien schreiben.

GDI+ speichert ein einzelnes Metadatenelement in einem PropertyItem Objekt. Sie können die PropertyItems Eigenschaft eines Image Objekts lesen, um alle Metadaten aus einer Datei abzurufen. Die PropertyItems -Eigenschaft gibt ein Array von PropertyItem -Objekten zurück.

Ein PropertyItem Objekt verfügt über die folgenden vier Eigenschaften: Id, , Value, Lenund Type.

Id

Ein Tag, das das Metadatenobjekt identifiziert. Einige Werte, die zu Id zugewiesen werden können, sind in der folgenden Tabelle gezeigt.

Hexadezimalwert BESCHREIBUNG
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
Bildtitel

Gerätehersteller

Gerätemodell

ExifDTOriginal

Exif-Belichtungszeit

Leuchtdichtetabelle

Chrominance-Tabelle

Wert

Ein Array von -Werten. Das Format der Werte wird durch die Type Eigenschaft bestimmt.

Len

Die Länge (in Byte) des Arrays von Werten, auf das von der Value Eigenschaft verwiesen wird.

Typ

Der Datentyp der Werte im Array, auf die von der Value Eigenschaft verwiesen wird. Die von den Type Eigenschaftswerten angegebenen Formate werden in der folgenden Tabelle angezeigt:

Numerischer Wert BESCHREIBUNG
1 Einen Byte
2 Ein Array von Objekten, die Byte als ASCII codiert sind
3 Eine 16-Bit-Ganzzahl
4 Eine 32-Bit-Ganzzahl
5 Ein Array von zwei Byte Objekten, die eine rationale Zahl darstellen
6 Nicht verwendet
7 Undefiniert
8 Nicht verwendet
9 SLong
10 SRational

Beispiel

Im folgenden Codebeispiel werden die sieben Metadatenelemente in der Datei FakePhoto.jpggelesen und angezeigt. Das zweite (Index 1)-Eigenschaftselement in der Liste weist Id 0x010F (Gerätehersteller) und Type 2 (ASCII-codiertes Bytearray) auf. Im Codebeispiel wird der Wert dieses Eigenschaftselements angezeigt.

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

Der Code erzeugt eine Ausgabe ähnlich der folgenden:

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.

Code kompilieren

Das vorangehende Beispiel wurde für die Verwendung mit Windows Forms entwickelt und erfordert PaintEventArgse, bei dem es sich um einen Parameter des Paint-Ereignishandlers handelt. Behandeln Sie das Ereignis des Formulars Paint , und fügen Sie diesen Code in den Paint-Ereignishandler ein. Sie müssen FakePhoto.jpg durch einen Bildnamen und -pfad ersetzen, der auf Ihrem System gültig ist, und den System.Drawing.Imaging-Namespace importieren.

Siehe auch