Como: Ler metadados de imagem
Alguns arquivos de imagem contêm metadados que você pode ler para determinar os recursos da imagem.Por exemplo, uma fotografia digital pode conter metadados que você pode ler para determinar a marca e modelo da câmera usada para capturar a imagem.Com GDI+, você pode ler os metadados existentes e você também pode escrever novos metadados para arquivos de imagem.
GDI+ armazena uma parte individual de metadados em um PropertyItem objeto. Você pode ler o PropertyItems propriedade de um Image objeto para recuperar todos os metadados de um arquivo. The PropertyItems propriedade retorna uma matriz de PropertyItem objetos.
A PropertyItem objeto tem quatro propriedades a seguintes: Id, Value, Len, e Type.
ID
Uma marca que identifica o item de metadados.Alguns valores podem ser atribuídos a Id são mostrados na tabela a seguir.
Valor hexadecimal |
Descrição |
---|---|
0x0320 0x010F 0 x 0110 0x9003 0x829A 0x5090 0x5091 |
Título da imagem Fabricante de equipamento Modelo de equipamento ExifDTOriginal time de exposição de EXIF Tabela de luminosidade Tabela Chrominance |
Valor
Uma matriz de valores.O formato dos valores é determinado pelo Type propriedade.
Com
O comprimento (em bytes) da matriz de valores apontado pelo Value propriedade.
Type (Tipo)
O tipo de dados de valores na matriz apontado pelo Value propriedade. Os formatos indicados pelo Type valores de propriedade são mostrados na tabela a seguir
Valor numérico |
Descrição |
---|---|
1 |
Um Byte |
2 |
Uma matriz de Byte objetos codificados em ASCII |
3 |
Um inteiro de 16 bit |
4 |
Um inteiro de 32 bit |
5 |
Uma matriz de dois Byte objetos que representam um número racional |
6 |
Não usado |
7 |
Undefined |
8 |
Não usado |
9 |
SLong |
10 |
SRational |
Exemplo
Descrição
O exemplo de código a seguir lê e exibe as sete partes dos metadados no arquivo FakePhoto.jpg. O segundo item de propriedade (índice 1) na lista possui Id 0x010F (fabricante do equipamento) e Type 2 (Matriz de byte codificado em ASCII). O exemplo de código exibe o valor do item propriedade.
O código produz saída similar à seguinte:
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.
Código
'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);
Compilando o código
The preceding example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler.Substituir FakePhoto.jpg com um nome de imagem e o caminho válido no seu sistema.