コード例 #1
0
ファイル: ExifEncoder.cs プロジェクト: twobob/exif-utils.net
        public static byte[] ConvertData(Type dataType, ExifType targetType, object value)
        {
            switch (targetType)
            {
            case ExifType.Ascii:
            {
                return(Encoding.ASCII.GetBytes(Convert.ToString(value) + '\0'));
            }

            case ExifType.Byte:
            case ExifType.Raw:
            {
                if (dataType == typeof(UnicodeEncoding))
                {
                    return(Encoding.Unicode.GetBytes(Convert.ToString(value) + '\0'));
                }

                return(ExifEncoder.WriteBytes(value));
            }

            case ExifType.Int32:
            {
                return(ExifEncoder.WriteInt32(value));
            }

            case ExifType.Rational:
            {
                return(ExifEncoder.WriteRational(value));
            }

            case ExifType.UInt16:
            {
                return(ExifEncoder.WriteUInt16(value));
            }

            case ExifType.UInt32:
            {
                return(ExifEncoder.WriteUInt32(value));
            }

            case ExifType.URational:
            {
                return(ExifEncoder.WriteURational(value));
            }

            default:
            {
                throw new NotImplementedException(String.Format("Encoding for EXIF type \"{0}\" has not yet been implemented.", targetType));
            }
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds an EXIF property to an image.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="property"></param>
        public static void AddExifData(Image image, ExifProperty property)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            if (property == null)
            {
                return;
            }

            PropertyItem propertyItem;

            // The .NET interface for GDI+ does not allow instantiation of the
            // PropertyItem class. Therefore one must be stolen off the Image
            // and repurposed.  GDI+ uses PropertyItem by value so there is no
            // side effect when changing the values and reassigning to the image.
            if (image.PropertyItems == null || image.PropertyItems.Length < 1)
            {
                propertyItem = ExifWriter.CreatePropertyItem();
            }
            else
            {
                propertyItem = image.PropertyItems[0];
            }

            propertyItem.Id   = (int)property.Tag;
            propertyItem.Type = (short)property.Type;

            Type dataType = ExifDataTypeAttribute.GetDataType(property.Tag);

            propertyItem.Value = ExifEncoder.ConvertData(dataType, property.Type, property.Value);
            propertyItem.Len   = propertyItem.Value.Length;

            // This appears to not be necessary
            ExifWriter.RemoveExifData(image, property.Tag);
            image.SetPropertyItem(propertyItem);
        }