【问题标题】:How to write EXIF data to an image in C#?如何在 C# 中将 EXIF 数据写入图像?
【发布时间】:2019-10-06 05:27:00
【问题描述】:

我很难将 EXIF 数据写入图像。到目前为止,我已经设法编写需要字符串作为输入的数据。但是,对于需要不同类型(例如曝光时间、快门速度)的元素,我不知道如何做到这一点。

我正在关注this 指南,但除了字符串之外没有其他示例。这个website 解释了我必须使用的数据类型,Microsoft documentation 提供了相应的数值。不幸的是,我无法使用它来解决我的问题。 为了找出哪个ID对应哪个值,我使用this列表和官方documentation

System.Drawing.Image imgEXIF = System.Drawing.Image.FromFile("D:/def.jpg");
System.Drawing.Image imgDummy = System.Drawing.Image.FromFile("D:/IMG_3214.jpg");
System.Drawing.Imaging.PropertyItem item = imgDummy.PropertyItems[0];
item.Id = 0x9286;
item.Type = 2; //String
item.Value = System.Text.Encoding.UTF8.GetBytes("Hello World\r\nthis is a test\0");
item.Len = item.Value.Length;
imgEXIF.SetPropertyItem(item);
imgEXIF.Save("D:/ghi.jpg");

任何关于如何编写不是字符串的 EXIF 数据的帮助将不胜感激!

【问题讨论】:

    标签: c# exif


    【解决方案1】:

    PropertyItem 上的 Value 中的内容是基于该字段对应的 Type 的字节数组。对于您已经可以执行的文本字段,该字节数组采用以空结尾的ASCII 字节数组的形式。我在TagTypes 下评论过的其他类型。对于曝光时间字段,这是一个 8 字节数组,由两个无符号 32 位整数组成 - 分子,后跟分母。我们可以使用BitConverter.GetBytes() 方法将uint(无符号32 位整数)转换为4 字节表示形式——然后简单地与另一个字节数组连接以获得分子和分母对。

    以下是一些扩展,它们展示了如何使用除了 Type 2 字符串字段之外的 Short / type 3 字段和 Rational / type 5 字段:

    public static class ImageMetaExtensions
    {
        public static void SetMaxAperture(this Image image, uint numerator, uint denominator)
        {
            SetMetaDataItem(image, MAX_APERTURE, (short)TagTypes.RATIONAL, GetPairUnsigned32Integer(numerator, denominator));
        }
    
        public static void SetExposureTime(this Image image, uint numerator, uint denominator)
        {
            SetMetaDataItem(image, EXPOSURE_TIME, (short)TagTypes.RATIONAL, GetPairUnsigned32Integer(numerator, denominator));
        }
    
        public static void SetUserComment(this Image image, string text)
        {
            SetMetaDataItem(image, USER_COMMENT, (short)TagTypes.ASCII, GetNullTerminatedString(text));
        }
    
        public static void Set35mmFocalLength(this Image image, short focalLength)
        {
            SetMetaDataItem(image, FOCALLENGTH_35MM, (short)TagTypes.SHORT, BitConverter.GetBytes(focalLength));
        }
    
        public enum TagTypes : short
        {
            BYTE = 1, // 8 bit unsigned integer
            ASCII = 2,
            SHORT = 3, // 16-bit unsigned integer
            LONG = 4, // 32-bit unsigned integer
            RATIONAL = 5, // two unsigned longs - first numerator, second denominator
            UNDEFINED = 6, // any value depending on field definition
            SLONG = 7, // signed 32-bit
            SRATIONAL = 10 // signed pair of 32-bit numerator/denominator
        }
    
        private static void SetMetaDataItem(Image image, int id, short type, byte[] data)
        {
            PropertyItem anyItem = image.PropertyItems[0];
            anyItem.Id = id;
            anyItem.Len = data.Length;
            anyItem.Type = type;
            anyItem.Value = data;
            image.SetPropertyItem(anyItem);
        }
    
        private static byte[] GetPairUnsigned32Integer(uint numerator, uint denominator)
        {
            return BitConverter.GetBytes(numerator).Concat(BitConverter.GetBytes(denominator)).ToArray();
        }
    
        private static byte[] GetNullTerminatedString(string text)
        {
            return Encoding.ASCII.GetBytes(text + "\0");
        }
    
        private const int EXPOSURE_TIME = 0x829A;      
        private const int USER_COMMENT = 0x9286;
        private const int MAX_APERTURE = 0x9205;
        private const int FOCALLENGTH_35MM = 0xA405;
    }
    

    用法:

    System.Drawing.Image myImage = System.Drawing.Image.FromFile(@"c:\temp\someimage.jpg");
    myImage.SetExposureTime(1, 30); // 1/30sec
    myImage.SetUserComment("Hello, world");
    myImage.Set35mmFocalLength(5);
    myImage.Save(@"c:\temp\someotherimage.jpg"); // save somewhere else
    

    【讨论】:

      猜你喜欢
      • 2015-02-28
      • 1970-01-01
      • 2018-08-30
      • 2021-12-27
      • 2012-05-30
      • 2013-11-10
      • 2017-09-11
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多