Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Collections 如何将多个属性集合传递给方法C#_Collections_Properties - Fatal编程技术网

Collections 如何将多个属性集合传递给方法C#

Collections 如何将多个属性集合传递给方法C#,collections,properties,Collections,Properties,我试图将一组对象传递给c#中的一个方法 以下是方法。 第一个期望传递单个属性 /// <summary> /// Adds an EXIF property to an image. /// </summary> /// <param name="inputPath">file path of original image</param> /// <param name="outputPath">file

我试图将一组对象传递给c#中的一个方法

以下是方法。 第一个期望传递单个属性

/// <summary>
    /// Adds an EXIF property to an image.
    /// </summary>
    /// <param name="inputPath">file path of original image</param>
    /// <param name="outputPath">file path of modified image</param>
    /// <param name="property"></param>
    public static void AddExifData(string inputPath, string outputPath, ExifProperty property)
    {
        using (Image image = Image.FromFile(inputPath))
        {
            ExifWriter.AddExifData(image, property);
            image.Save(outputPath);
        }
    }
这里我只向方法传递一个属性。如何将多个项目发送到这样的方法

// add copyright tag
ExifProperty copyright = new ExifProperty();
copyright.Tag = ExifTag.Copyright;
copyright.Value = String.Format(
       "Copyright (c){0} Lorem ipsum dolor sit amet. All rights reserved.",
       DateTime.Now.Year);

// Add folder date to exif tag
ExifProperty folderDate = new ExifProperty();
folderDate.Tag = ExifTag.DateTime;
folderDate.Value = lastPart.ToString();
然后传递这两个属性

ExifWriter.AddExifData(imagePath, outputPath, ??????????);
谢谢

您正在尝试创建一个

您正在尝试创建一个新的


您可以使用
params
关键字:

public static void AddExifData(
    string inputPath, 
    string outputPath,
    params ExifProperty[] properties)
{
    using (Image image = Image.FromFile(inputPath))
    {
        ExifWriter.AddExifData(image, new ExifPropertyCollection(properties));
        image.Save(outputPath);
    }
}
然后打电话:

ExifWriter.AddExifData(imagePath, outputPath, copyright, folderDate);

您可以使用
params
关键字:

public static void AddExifData(
    string inputPath, 
    string outputPath,
    params ExifProperty[] properties)
{
    using (Image image = Image.FromFile(inputPath))
    {
        ExifWriter.AddExifData(image, new ExifPropertyCollection(properties));
        image.Save(outputPath);
    }
}
然后打电话:

ExifWriter.AddExifData(imagePath, outputPath, copyright, folderDate);

下面是未添加新代码的完整类

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;

namespace ExifUtils.Exif.IO
{
    /// <summary>
    /// Utility class for writing EXIF data
    /// </summary>
    public static class ExifWriter
    {
        #region Fields

        private static ConstructorInfo ctorPropertyItem = null;

        #endregion Fields

        #region Write Methods

        /// <summary>
        /// Adds a collection of EXIF properties to an image.
        /// </summary>
        /// <param name="inputPath">file path of original image</param>
        /// <param name="outputPath">file path of modified image</param>
        /// <param name="properties"></param>
        public static void AddExifData(string inputPath, string outputPath, ExifPropertyCollection properties)
        {
            using (Image image = Image.FromFile(inputPath))
            {
                ExifWriter.AddExifData(image, properties);
                image.Save(outputPath);
            }
        }

        /// <summary>
        /// Adds an EXIF property to an image.
        /// </summary>
        /// <param name="inputPath">file path of original image</param>
        /// <param name="outputPath">file path of modified image</param>
        /// <param name="property"></param>
        public static void AddExifData(string inputPath, string outputPath, ExifProperty property)
        {
            using (Image image = Image.FromFile(inputPath))
            {
                ExifWriter.AddExifData(image, property);
                image.Save(outputPath);
            }
        }

        /// <summary>
        /// Adds a collection of EXIF properties to an image.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="properties"></param>
        public static void AddExifData(Image image, ExifPropertyCollection properties)
        {
            if (image == null)
            {
                throw new NullReferenceException("image was null");
            }

            if (properties == null || properties.Count < 1)
            {
                return;
            }

            foreach (ExifProperty property in properties)
            {
                ExifWriter.AddExifData(image, property);
            }
        }

        /// <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 NullReferenceException("image was null");
            }

            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);

            switch (property.Type)
            {
                case ExifType.Ascii:
                    {
                        propertyItem.Value = Encoding.ASCII.GetBytes(Convert.ToString(property.Value) + '\0');
                        break;
                    }
                case ExifType.Byte:
                    {
                        if (dataType == typeof(UnicodeEncoding))
                        {
                            propertyItem.Value = Encoding.Unicode.GetBytes(Convert.ToString(property.Value) + '\0');
                        }
                        else
                        {
                            goto default;
                        }
                        break;
                    }
                default:
                    {
                        throw new NotImplementedException(String.Format("Encoding for EXIF property \"{0}\" has not yet been implemented.", property.DisplayName));
                    }
            }
            propertyItem.Len = propertyItem.Value.Length;

            // This appears to not be necessary
            //foreach (int id in image.PropertyIdList)
            //{
            //    if (id == exif.PropertyItem.Id)
            //    {
            //        image.RemovePropertyItem(id);
            //        break;
            //    }
            //}
            image.SetPropertyItem(propertyItem);
        }

        #endregion Write Methods

        #region Copy Methods

        /// <summary>
        /// Copies EXIF data from one image to another
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        public static void CloneExifData(Image source, Image dest)
        {
            ExifWriter.CloneExifData(source, dest, -1);
        }

        /// <summary>
        /// Copies EXIF data from one image to another
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <param name="maxPropertyBytes">setting to filter properties</param>
        public static void CloneExifData(Image source, Image dest, int maxPropertyBytes)
        {
            bool filter = (maxPropertyBytes > 0);

            // preserve EXIF
            foreach (PropertyItem prop in source.PropertyItems)
            {
                if (filter && prop.Len > maxPropertyBytes)
                {
                    // skip large sections
                    continue;
                }

                dest.SetPropertyItem(prop);
            }
        }

        #endregion Copy Methods

        #region Utility Methods

        /// <summary>
        /// Uses Reflection to instantiate a PropertyItem
        /// </summary>
        /// <returns></returns>
        internal static PropertyItem CreatePropertyItem()
        {
            if (ExifWriter.ctorPropertyItem == null)
            {
                // Must use Reflection to get access to PropertyItem constructor
                ExifWriter.ctorPropertyItem = typeof(PropertyItem).GetConstructor(Type.EmptyTypes);
                if (ExifWriter.ctorPropertyItem == null)
                {
                    throw new NotSupportedException("Unable to instantiate a System.Drawing.Imaging.PropertyItem");
                }
            }

            return (PropertyItem)ExifWriter.ctorPropertyItem.Invoke(null);
        }

        #endregion Utility Methods
    }
}
使用系统;
使用系统文本;
使用系统图;
使用系统、绘图、成像;
运用系统反思;
命名空间ExifUtils.Exif.IO
{
/// 
///用于写入EXIF数据的实用程序类
/// 
公共静态类ExifWriter
{
#区域字段
私有静态构造函数InfoctorPropertyItem=null;
#端域字段
#区域写入方法
/// 
///将EXIF属性集合添加到图像。
/// 
///原始图像的文件路径
///修改图像的文件路径
/// 
公共静态void AddExifData(字符串输入路径、字符串输出路径、ExifPropertyCollection属性)
{
使用(Image=Image.FromFile(inputPath))
{
ExifWriter.AddExifData(图像、属性);
image.Save(输出路径);
}
}
/// 
///将EXIF属性添加到图像。
/// 
///原始图像的文件路径
///修改图像的文件路径
/// 
公共静态void AddExifData(字符串输入路径、字符串输出路径、ExifProperty属性)
{
使用(Image=Image.FromFile(inputPath))
{
ExifWriter.AddExifData(图像、属性);
image.Save(输出路径);
}
}
/// 
///将EXIF属性集合添加到图像。
/// 
/// 
/// 
公共静态void AddExifData(图像、ExifPropertyCollection属性)
{
if(image==null)
{
抛出新的NullReferenceException(“图像为null”);
}
if(properties==null | | properties.Count<1)
{
返回;
}
foreach(属性中的ExifProperty属性)
{
ExifWriter.AddExifData(图像、属性);
}
}
/// 
///将EXIF属性添加到图像。
/// 
/// 
/// 
公共静态void AddExifData(图像,ExifProperty属性)
{
if(image==null)
{
抛出新的NullReferenceException(“图像为null”);
}
if(属性==null)
{
返回;
}
PropertyItem PropertyItem;
//GDI+的.NET接口不允许实例化
//PropertyItem类。因此必须从映像中偷取一个
//GDI+按值使用PropertyItem,因此没有
//更改值并重新指定给图像时的副作用。
if(image.PropertyItems==null | | image.PropertyItems.Length<1)
{
propertyItem=ExifWriter.CreatePropertyItem();
}
其他的
{
propertyItem=image.PropertyItems[0];
}
propertyItem.Id=(int)property.Tag;
propertyItem.Type=(短)property.Type;
Type dataType=ExifDataTypeAttribute.GetDataType(property.Tag);
开关(属性.类型)
{
case ExifType.Ascii:
{
propertyItem.Value=Encoding.ASCII.GetBytes(Convert.ToString(property.Value)+'\0');
打破
}
案例ExifType.Byte:
{
if(数据类型==typeof(Unicode编码))
{
propertyItem.Value=Encoding.Unicode.GetBytes(Convert.ToString(property.Value)+'\0');
}
其他的
{
后藤违约;
}
打破
}
违约:
{
抛出新的NotImplementedException(String.Format(“EXIF属性\“{0}\”的编码尚未实现。”,property.DisplayName));
}
}
propertyItem.Len=propertyItem.Value.Length;
//这似乎没有必要
//foreach(image.PropertyIdList中的int-id)
//{
//if(id==exif.PropertyItem.id)
//    {
//image.RemovePropertyItem(id);
//中断;
//    }
//}
image.SetPropertyItem(propertyItem);
}
#端域写入方法
#区域复制方法
/// 
///将EXIF数据从一个图像复制到另一个图像
/// 
/// 
/// 
公共静态void CloneExifData(图像源、图像目标)
{
ExifWriter.CloneExifData(源,目标,-1);
}
/// 
///将EXIF数据从一个图像复制到另一个图像
/// 
/// 
/// 
///设置过滤器属性
公共静态void CloneExifData(图像源、图像目标、int maxPropertyBytes)
{
布尔过滤器=(maxPropertyBytes>0);
//保护进出口银行
foreach(source.PropertyItems中的PropertyItem属性)
{
if(过滤器和属性Len>maxPropertyBytes)
using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;

namespace ExifUtils.Exif.IO
{
    /// <summary>
    /// Utility class for writing EXIF data
    /// </summary>
    public static class ExifWriter
    {
        #region Fields

        private static ConstructorInfo ctorPropertyItem = null;

        #endregion Fields

        #region Write Methods

        /// <summary>
        /// Adds a collection of EXIF properties to an image.
        /// </summary>
        /// <param name="inputPath">file path of original image</param>
        /// <param name="outputPath">file path of modified image</param>
        /// <param name="properties"></param>
        public static void AddExifData(string inputPath, string outputPath, ExifPropertyCollection properties)
        {
            using (Image image = Image.FromFile(inputPath))
            {
                ExifWriter.AddExifData(image, properties);
                image.Save(outputPath);
            }
        }

        /// <summary>
        /// Adds an EXIF property to an image.
        /// </summary>
        /// <param name="inputPath">file path of original image</param>
        /// <param name="outputPath">file path of modified image</param>
        /// <param name="property"></param>
        public static void AddExifData(string inputPath, string outputPath, ExifProperty property)
        {
            using (Image image = Image.FromFile(inputPath))
            {
                ExifWriter.AddExifData(image, property);
                image.Save(outputPath);
            }
        }

        /// <summary>
        /// Adds a collection of EXIF properties to an image.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="properties"></param>
        public static void AddExifData(Image image, ExifPropertyCollection properties)
        {
            if (image == null)
            {
                throw new NullReferenceException("image was null");
            }

            if (properties == null || properties.Count < 1)
            {
                return;
            }

            foreach (ExifProperty property in properties)
            {
                ExifWriter.AddExifData(image, property);
            }
        }

        /// <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 NullReferenceException("image was null");
            }

            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);

            switch (property.Type)
            {
                case ExifType.Ascii:
                    {
                        propertyItem.Value = Encoding.ASCII.GetBytes(Convert.ToString(property.Value) + '\0');
                        break;
                    }
                case ExifType.Byte:
                    {
                        if (dataType == typeof(UnicodeEncoding))
                        {
                            propertyItem.Value = Encoding.Unicode.GetBytes(Convert.ToString(property.Value) + '\0');
                        }
                        else
                        {
                            goto default;
                        }
                        break;
                    }
                default:
                    {
                        throw new NotImplementedException(String.Format("Encoding for EXIF property \"{0}\" has not yet been implemented.", property.DisplayName));
                    }
            }
            propertyItem.Len = propertyItem.Value.Length;

            // This appears to not be necessary
            //foreach (int id in image.PropertyIdList)
            //{
            //    if (id == exif.PropertyItem.Id)
            //    {
            //        image.RemovePropertyItem(id);
            //        break;
            //    }
            //}
            image.SetPropertyItem(propertyItem);
        }

        #endregion Write Methods

        #region Copy Methods

        /// <summary>
        /// Copies EXIF data from one image to another
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        public static void CloneExifData(Image source, Image dest)
        {
            ExifWriter.CloneExifData(source, dest, -1);
        }

        /// <summary>
        /// Copies EXIF data from one image to another
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <param name="maxPropertyBytes">setting to filter properties</param>
        public static void CloneExifData(Image source, Image dest, int maxPropertyBytes)
        {
            bool filter = (maxPropertyBytes > 0);

            // preserve EXIF
            foreach (PropertyItem prop in source.PropertyItems)
            {
                if (filter && prop.Len > maxPropertyBytes)
                {
                    // skip large sections
                    continue;
                }

                dest.SetPropertyItem(prop);
            }
        }

        #endregion Copy Methods

        #region Utility Methods

        /// <summary>
        /// Uses Reflection to instantiate a PropertyItem
        /// </summary>
        /// <returns></returns>
        internal static PropertyItem CreatePropertyItem()
        {
            if (ExifWriter.ctorPropertyItem == null)
            {
                // Must use Reflection to get access to PropertyItem constructor
                ExifWriter.ctorPropertyItem = typeof(PropertyItem).GetConstructor(Type.EmptyTypes);
                if (ExifWriter.ctorPropertyItem == null)
                {
                    throw new NotSupportedException("Unable to instantiate a System.Drawing.Imaging.PropertyItem");
                }
            }

            return (PropertyItem)ExifWriter.ctorPropertyItem.Invoke(null);
        }

        #endregion Utility Methods
    }
}