2011-06-10 67 views
1

我想将对象的集合传递给c#中的方法。如何将多个属性集合传递给方法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); 
     } 
    } 

第二个需要一组属性。这也是我想要传递数据的方法。

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

要将数据作为单个属性传递,我使用此代码。

// Add folder date to exif tag 
ExifProperty folderDate = new ExifProperty(); 
folderDate.Tag = ExifTag.DateTime; 
folderDate.Value = lastPart.ToString(); 

ExifWriter.AddExifData(imagePath, outputPath, copyright); 

在这里,我只传递一个属性的方法。我怎么能发送多个项目到这样的方法。

// 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, ??????????); 

感谢

回答

1

你想创建一个params ExifProperty[] parameter

public static void AddExifData(string inputPath, string outputPath, params ExifProperty[] properties) 
{ ... } 

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

+1比我快一点! – aligray 2011-06-10 02:37:43

0

您可以使用关键字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); 
+0

谢谢你们,不幸的是这些答案没有奏效。我收到错误。 'ExifUtils.Exif.IO.ExifWriter.AddExifdata(system.Frawing.Image,ExifUtils.Exif.ExifPropertyCollection)'的最佳重载方法匹配有一些无效的参数 – Jason 2011-06-10 02:49:32

+0

@Jason我没有意识到你在问什么在做什么该方法本身也是如此。也许最好的做法是在方法调用之外创建一个'ExifPropertyCollection'并将其作为参数传递。 – aligray 2011-06-10 02:53:29

+0

@Jason或者,找到一种方法将'ExifProperty []'转换为'ExifPropertyCollection'。 – aligray 2011-06-10 02:54:02

0

这里是没有添加新的代码满级。

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 
    } 
} 
相关问题