/// 照片元数据编码
在下面的文章里,可以看到图片所有的元数据定义信息
元数据的结果类型定义
/// 数据类型结果
Integer | 表示的类型 |
---|---|
1 | 指定 Value 为字节数组。 |
2 | 指定 Value 为空终止 ASCII 字符串。如果将类型数据成员设置为 ASCII 类型,则应该将 Len 属性设置为包括空终止的字符串长度。例如,字符串“Hello”的长度为 6。 |
3 | 指定 Value 为无符号的短(16 位)整型数组。 |
4 | 指定 Value 为无符号的长(32 位)整型数组。 |
5 | 指定 Value 数据成员为无符号的长整型对数组。每一对都表示一个分数;第一个整数是分子,第二个整数是分母。 |
6 | 指定 Value 为可以包含任何数据类型的值的字节数组。 |
7 | 指定 Value 为有符号的长(32 位)整型数组。 |
10 | 指定 Value 为有符号的长整型对数组。每一对都表示一个分数;第一个整数是分子,第二个整数是分母。 |
操作元数据的一般步骤:
1:读取图片得到Bitmap
2:得到一个PropertyItem对象, PropertyItem propertyItem = image.PropertyItems[0];
3:为该PropertyItem对象设置Id ,Value ,Type ,Len 等属性(查表看格式和Id)
4:将值保存 image.SetPropertyItem(propertyItem);
5:保存图片,就可以得到包含指定信息的图片文件了。
不用担心覆盖的问题,不知道为啥微软不让创建PropertyItem对象,而通过这种方式,来为照片添加元数据信息。
通过元数据,我们可以对照片进行很多后期处理,比如根据照相时间重新命名文件,给照片添加版权信息,添加描述等等信息。
写入经纬度信息
/// PropertyTagGpsLatitudeRef SetPropertyString(bitmap, 0x0001, "N"); /// PropertyTagGpsLongitudeRef SetPropertyString(bitmap, 0x0003, "E"); byte[] bytes = new byte[24]; byte[] bytes1 = BitConverter.GetBytes(39); byte[] bytes2 = BitConverter.GetBytes(1); byte[] bytes3 = BitConverter.GetBytes(36); byte[] bytes4 = BitConverter.GetBytes(1); byte[] bytes5 = BitConverter.GetBytes(1256789553); byte[] bytes6 = BitConverter.GetBytes(10000000); int index = 0; bytes1.CopyTo(bytes, index); index += 4; bytes2.CopyTo(bytes, index); index += 4; bytes3.CopyTo(bytes, index); index += 4; bytes4.CopyTo(bytes, index); index += 4; bytes5.CopyTo(bytes, index); index += 4; bytes6.CopyTo(bytes, index); /// PropertyTagGpsLatitude SetProperty(bitmap, 0x0002, bytes, 5); bytes = new byte[24]; bytes1 = BitConverter.GetBytes(116); bytes2 = BitConverter.GetBytes(1); bytes3 = BitConverter.GetBytes(21); bytes4 = BitConverter.GetBytes(1); bytes5 = BitConverter.GetBytes(2196657632); bytes6 = BitConverter.GetBytes(10000000); index = 0; bytes1.CopyTo(bytes, index); index += 4; bytes2.CopyTo(bytes, index); index += 4; bytes3.CopyTo(bytes, index); index += 4; bytes4.CopyTo(bytes, index); index += 4; bytes5.CopyTo(bytes, index); index += 4; bytes6.CopyTo(bytes, index); /// PropertyTagGpsLongitude SetProperty(bitmap, 0x0004, bytes, 5);
public void SetProperty(Bitmap image, Int32 propertyItemId, byte[] bytes, short propertyItemType) { System.Drawing.Imaging.PropertyItem propertyItem = (System.Drawing.Imaging.PropertyItem)(System.Activator.CreateInstance(typeof(System.Drawing.Imaging.PropertyItem), true)); propertyItem.Id = propertyItemId; propertyItem.Value = bytes; propertyItem.Type = propertyItemType; propertyItem.Len = bytes.Length; image.SetPropertyItem(propertyItem); }