Condividi tramite


Procedura: leggere i metadati delle immagini

Aggiornamento: novembre 2007

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 dispone delle seguenti quattro proprietà: Id, Value, Len e Type.

Id

Tag che identifica l'elemento dei metadati. Nella tabella seguente 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 a cui fa riferimento la proprietà Value. I formati indicati dai valori della proprietà Type sono elencati nella tabella seguente.

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

Esempio

Descrizione

Nell'esempio di codice riportato di seguito vengono letti e visualizzati i sette metadati del file FakePhoto.jpg. Il secondo elemento della proprietà (indice 1) dell'elenco contiene Id 0x010F (produttore dell'attrezzatura) e Type 2 (matrice di byte codificata ASCII). Nel codice di esempio viene mostrato il valore di tale elemento della proprietà.

L'output prodotto dal codice è 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.

Codice

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

Compilazione del codice

L'esempio riportato in precedenza è stato creato per essere utilizzato con Windows Form e richiede PaintEventArgs e, un parametro del gestore eventi Paint. Sostituire FakePhoto.jpg con il nome e il percorso di un'immagine validi nel computer in uso.

Vedere anche

Altre risorse

Immagini, bitmap e metafile

Utilizzo di immagini, bitmap, icone e metafile