Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Algunos archivos de imagen contienen metadatos que puede leer para determinar las características de la imagen. Por ejemplo, una fotografía digital puede contener metadatos que puede leer para determinar la marca y el modelo de la cámara usada para capturar la imagen. Con GDI+, puede leer metadatos existentes y también puede escribir nuevos metadatos en archivos de imagen.
GDI+ almacena un fragmento individual de metadatos en un PropertyItem objeto . Puede leer la PropertyItems propiedad de un Image objeto para recuperar todos los metadatos de un archivo. La PropertyItems propiedad devuelve una matriz de PropertyItem objetos .
Un PropertyItem objeto tiene las cuatro propiedades siguientes: Id
, Value
, Len
y Type
.
ID
Etiqueta que identifica el elemento de metadatos. Algunos valores a los Id que se puede asignar se muestran en la tabla siguiente:
Valor hexadecimal | Descripción |
---|---|
0x0320 0x010F 0x0110 0x9003 0x829A 0x5090 0x5091 |
Título de la imagen Fabricante de equipos Modelo de equipo ExifDTOriginal Tiempo de exposición de exif Tabla de luminancia Tabla de Crominancia |
Importancia
Matriz de valores. El formato de los valores viene determinado por la Type propiedad .
Len
Longitud (en bytes) de la matriz de valores a los que apunta la Value propiedad .
Tipo
Tipo de datos de los valores en la matriz a la que apunta la propiedad Value
. Los formatos indicados por los valores de propiedad Type
se muestran en la tabla siguiente:
Valor numérico | Descripción |
---|---|
1 | Una operación Byte |
2 | Matriz de Byte objetos codificados como ASCII |
3 | Entero de 16 bits |
4 | Entero de 32 bits |
5 | Matriz de dos Byte objetos que representan un número racional |
6 | No se usa |
7 | Indefinido |
8 | No se usa |
9 | SLong |
10 | SRational |
Ejemplo
En el ejemplo de código siguiente se leen y se muestran los siete fragmentos de metadatos del archivo FakePhoto.jpg
. El segundo elemento de propiedad (índice 1) de la lista tiene Id 0x010F (fabricante de equipos) y Type 2 (matriz de bytes codificada en ASCII). El ejemplo de código muestra el valor de ese elemento de propiedad.
// 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)
El código genera una salida similar a la siguiente:
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.
Compilar el código
El ejemplo anterior está diseñado para su uso con Windows Forms y requiere PaintEventArgse
, que es un parámetro del Paint controlador de eventos. Controle el evento del Paint formulario y pegue este código en el controlador de eventos de pintura. Debe reemplazar el FakePhoto.jpg
con un nombre de imagen y una ruta de acceso válidos en su sistema e importar el espacio de nombres System.Drawing.Imaging
.
Consulte también
.NET Desktop feedback