Freigeben über


Lesen von Metadaten

Einige Bilddateien enthalten lesbare Metadaten, mit deren Hilfe Features des Bildes bestimmt werden können. Ein digitales Foto kann beispielsweise Metadaten enthalten, die Sie auslesen können, um Fabrikat und Modell der Kamera zu ermitteln, mit der das Bild aufgenommen wurde. Mit Hilfe von GDI+ können vorhandene Metadaten gelesen sowie neue Metadaten in Bilddateien geschrieben werden.

GDI+ speichert individuelle Metadaten in einem PropertyItem-Objekt. Um sämtliche Metadaten aus einer Datei zu erhalten, kann die PropertyItems-Eigenschaft eines Image-Objekts gelesen werden. Die PropertyItems-Eigenschaft gibt ein Array von PropertyItem-Objekten zurück.

Ein PropertyItem-Objekt verfügt über die folgenden vier Eigenschaften:

Id

Ein Tag, das das Metadatenelement identifiziert. In der folgenden Tabelle sind einige Werte aufgelistet, die der Id-Eigenschaft zugewiesen werden können.

Hexadezimalwert Beschreibung
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

Bildtitel

Gerätehersteller

Gerätemodell

ExifDTOriginal

Exif-Belichtungszeit

Luminanztabelle

Chrominanztabelle

Value

Ein Wertearray; das Format der Werte wird durch die Type-Eigenschaft bestimmt.

Len

Die Länge des Wertearrays, auf das die Value-Eigenschaft zeigt (in Bytes).

Type

Der Datentyp der Werte im Array, auf das die Value-Eigenschaft zeigt. In der folgenden Tabelle sind die durch die Type-Eigenschaftenwerte angegebenen Formate aufgeführt.

Numerischer Wert Beschreibung
1 Byte
2 Byteobjektarray, als ASCII codiert
3 Ganze 16-Bit-Zahl
4 Ganze 32-Bit-Zahl
5 Array aus zwei Byteobjekten, die eine rationale Zahl darstellen
6 Nicht in Verwendung
7 Nicht definiert
8 Nicht in Verwendung
9 SLong
10 SRational

Im folgenden Beispiel werden die sieben Metadatenelemente in der Datei FakePhoto.jpg eingelesen und angezeigt.

'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++;
}

Durch den vorangehenden Code wird eine mit folgendem Beispiel vergleichbare Ausgabe generiert:

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

Das zweite Eigenschaftenelement in der Liste (Index 1) verfügt über Id 0x010F (Gerätehersteller) und Type 2 (ASCII-codiertes Bytearray). Durch folgenden Code, der eine Fortsetzung des vorherigen Codes darstellt, wird der Wert dieses Eigenschaftenelements angezeigt:

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

Durch den vorangehenden Code wird eine mit folgendem Beispiel vergleichbare Ausgabe generiert:

The equipment make is Northwind Camera.