如何:读取图像元数据

某些图像文件包含可用于确定图像功能的元数据。 例如,数字照片可能包含可以读取的元数据,以确定用于捕获图像的相机的制作和模型。 使用 GDI+,可以读取现有元数据,还可以将新元数据写入图像文件。

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

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

Id

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

十六进制值 DESCRIPTION
0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091
图像标题

设备制造商

设备型号

ExifDTOriginal

Exif 曝光时间

亮度表

色度表

价值

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

莱恩

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

类型

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

数值 DESCRIPTION
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 命名空间。

另请参阅