读写图片元数据

来源:百度知道 编辑:UC知道 时间:2024/06/18 15:03:20
C#.net读写tiff和jpg格式的图片元数据,能否给个例子,分可以再加。

//获取图像文件的所有元数据属性,以PropertyItem数组的格式保存
public static PropertyItem[] GetExifProperties(string fileName)
{
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//通过指定的数据流来创建Image
System.Drawing.Image image = System.Drawing.Image.FromStream(stream,true,false);
return image.PropertyItems;
}
获得所有元数据后,需要挑选出我们所感兴趣的拍照日期/时间属性所对应的值,代码如下:
//遍历所有元数据,获取拍照日期/时间
private string GetTakePicDateTime(System.Drawing.Imaging.PropertyItem[] parr)
{
Encoding ascii = Encoding.ASCII ;
//遍历图像文件元数据,检索所有属性
foreach (System.Drawing.Imaging.PropertyItem p in parr)
{
//如果是PropertyTagDateTime,则返回该属性所对应的值
if (p.Id==0x0132)
{
return ascii.GetString(p.Value);
}
}
//若没有相关的EXIF信息则返回N/A
return "N/A";
}