Share via


Lettura di metadati

Alcuni file di immagine contengono metadati leggibili per determinare le caratteristiche di un'immagine. Una fotografia digitale, ad esempio, potrebbe contenere metadati leggibili per determinare produttore e modello della macchina fotografica utilizzata per scattare la fotografia. Con GDI+ è possibile leggere i metadati esistenti, oltre a scrivere nuovi metadati in file di immagine.

Con GDI+ si memorizza un singolo metadato in un oggetto PropertyItem. È possibile leggere la proprietà PropertyItems di un oggetto Image per recuperare tutti i metadati da un file. La proprietà PropertyItems restituisce una matrice di oggetti PropertyItem.

Un oggetto PropertyItem possiede le quattro proprietà che seguono:

Id

Tag che identifica l'elemento dei metadati. Nella tabella che segue sono indicati alcuni valori che è possibile assegnare a Id.

Valore esadecimale Descrizione
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

Titolo dell'immagine

Produttore dell'apparecchio

Modello dell'apparecchio

ExifDTOriginal

Tempo di esposizione del file exif

Tabella di luminanza

Tabella di saturazione

Value

Matrice di valori. Il formato dei valori è determinato dalla proprietà Type.

Len

La lunghezza espressa in byte della matrice di valori cui fa riferimento la proprietà Value.

Type

Il tipo di dati dei valori nella matrice cui fa riferimento la proprietà Value. I formati indicati dai valori della proprietà Type sono indicati nella tabella che segue.

Valore numerico Descrizione
1 Un byte
2 Una matrice di oggetti Byte codificati in ASCII
3 Un integer a 16 bit
4 Un integer a 32 bit
5 Una matrice di due oggetti Byte che rappresenta un numero razionale
6 Non utilizzato
7 Non definito
8 Non utilizzato
9 SLong
10 SRational

Nell'esempio che segue vengono letti e visualizzati i sette metadati del file FakePhoto.jpg.

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

L'output prodotto dal codice precedente è analogo a quello che segue:

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

Il secondo elemento della proprietà (indice 1) dell'elenco contiene Id 0x010F (produttore dell'attrezzatura) e Type 2 (matrice di byte codificata ASCII). Con il codice che segue, che rappresenta una continuazione dell'esempio precedente, viene visualizzato il valore di quell'elemento della proprietà:

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

L'output prodotto dal codice precedente è analogo a quello che segue:

The equipment make is Northwind Camera.