如何:读取图像元数据

如何:读取图像元数据

.NET Framework 2.0
 
此主题尚未评级 - 评价此主题
 

一些图像文件中包含可供您读取以确定图像特征的元数据。例如,数字照片中可能包含可供您读取以确定用于捕获该图像的照相机的品牌和型号的元数据。利用 GDI+,可以读取现有的元数据,也可以将新的元数据写入图像文件中。

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

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

Id

用于标识元数据项的标记。下表显示一些可赋予 Id 的值。

 
十六进制值说明

0x0320

0x010F

0x0110

0x9003

0x829A

0x5090

0x5091

图像标题

设备制造商

设备型号

ExifDTOriginal

Exif 曝光时间

亮度表

色度表

Value

数组值。这些值的格式由 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 文件中的七段元数据。该列表中的第二个 (index 1) 属性项包含 Id 0x010F(设备制造商)和 Type 2(ASCII 编码的字节数组)。代码示例显示该属性项的值。

该代码将生成类似以下内容的输出:

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.

复制
// 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);

编译代码

前面的示例是为使用 Windows 窗体而设计的,它需要 Paint 事件处理程序的参数 PaintEventArgs e。用系统上有效的图像名称和路径替换 FakePhoto.jpg

原文地址:https://www.cnblogs.com/xianyin05/p/3075849.html