Procedura: leggere i metadati delle immagini

Alcuni file di immagine contengono metadati che è possibile leggere per determinare le funzionalità dell'immagine. Ad esempio, una fotografia digitale può contenere metadati che è possibile leggere per determinare la creazione e il modello della fotocamera usata per acquisire l'immagine. Con GDI+, è possibile leggere i metadati esistenti ed è anche possibile scrivere nuovi metadati nei file di immagine.

GDI+ archivia una singola parte di metadati in un PropertyItem oggetto . È possibile leggere la PropertyItems proprietà di un Image oggetto per recuperare tutti i metadati da un file. La PropertyItems proprietà restituisce una matrice di PropertyItem oggetti.

Un PropertyItem oggetto ha le quattro proprietà seguenti: Id, Value, Lene Type.

ID.

Tag che identifica l'elemento di metadati. Alcuni valori a Id cui è possibile assegnare sono indicati nella tabella seguente:

Valore esadecimale Descrizione
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
Titolo immagine

Produttore di apparecchiature

Modello di apparecchiature

ExifDTOriginal

Tempo di esposizione exif

Tabella di dominanza

Tabella di dominanza

Valore

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

Len

Lunghezza in byte della matrice di valori a cui punta la Value proprietà .

Tipo

Tipo di dati dei valori nella matrice a cui punta la Value proprietà . I formati indicati dai valori delle Type proprietà sono illustrati nella tabella seguente:

Valore numerico Descrizione
1 Comando Byte
2 Matrice di Byte oggetti codificati come ASCII
3 Intero a 16 bit
4 Intero a 32 bit
5 Matrice di due Byte oggetti che rappresentano un numero razionale
6 Non usato
7 Non definito
8 Non usato
9 SLong
10 SRational

Esempio

Nell'esempio di codice seguente vengono letti e visualizzati i sette metadati nel file FakePhoto.jpg. Il secondo elemento della proprietà (indice 1) nell'elenco ha Id 0x010F (produttore di apparecchiature) e Type 2 (matrice di byte con codifica ASCII). Nell'esempio di codice viene visualizzato il valore dell'elemento della proprietà.

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

Il codice genera un output simile al seguente:

 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.

Compilazione del codice

L'esempio precedente è progettato per l'uso con Windows Form e richiede PaintEventArgse, un parametro del gestore eventi Paint. Gestire l'evento del Paint modulo e incollare questo codice nel gestore eventi paint. È necessario sostituire FakePhoto.jpg con un nome e un percorso di immagine validi nel sistema e importare lo spazio dei System.Drawing.Imaging nomi.

Vedi anche