照片元数据信息以及在照片中写入gps信息

/// 照片元数据编码
在下面的文章里,可以看到图片所有的元数据定义信息
 
 
 元数据的结果类型定义
/// 数据类型结果
 
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);
        }

 

 
 

转载于:https://www.cnblogs.com/firesword/p/6093110.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值