如何:读取图像元数据

某些图像文件包含可以读取以确定图像特征的元数据。 例如,数码照片可能包含可以读取以确定用于捕获图像的相机的品牌和型号。 借助 GDI+,可以读取现有元数据,还可以将新的元数据写入图像文件。

GDI+ 将单个元数据片段存储在 PropertyItem 对象中。 可以读取 Image 对象的 PropertyItems 属性来从文件检索所有元数据。 PropertyItems 属性返回 PropertyItem 对象组成的一个数组。

一个 PropertyItem 对象具有以下四个属性:IdValueLenType

Id

标识元数据项的标记。 下表显示了可分配给 Id 的一些值:

十六进制值 说明
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
图像标题

设备制造商

设备型号

ExifDTOriginal

Exif 曝光时间

亮度表

色度表

一个 值数组。 值的格式由 Type 属性确定。

Len

Value 属性指向的值的数组的长度(以字节为单位)。

类型

Value 属性指向的数组中的值的数据类型。 下表显示了 Type 属性值指示的格式:

数值 说明
1 执行 Byte 操作
2 编码为 ASCII 的 Byte 对象组成的数组
3 16 位整数
4 32 位整数
5 表示有理数的两个 Byte 对象组成的数组
6 未使用
7 未定义
8 未使用
9 SLong
10 SRational

示例

下面的代码示例读取并显示文件 FakePhoto.jpg 中的七个元数据片段。 列表中的第二个(索引 1)属性项具有 Id 0x010F(设备制造商)和 Type 2(ASCII 编码的字节数组)。 此代码示例显示该属性项的值。

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

代码生成类似下面的输出:

 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.

编译代码

前面的示例专用于 Windows 窗体,它需要 PaintEventArgse,后者是 Paint 事件处理程序的参数。 处理窗体的 Paint 事件,并将此代码粘贴到画图事件处理程序中。 必须将 FakePhoto.jpg 替换为系统上有效的图像名称和路径,并导入 System.Drawing.Imaging 命名空间。

另请参阅