【问题标题】:C# Add extra property/info to a ImageC# 向图像添加额外的属性/信息
【发布时间】:2017-09-26 07:51:51
【问题描述】:

我想为图像添加额外的属性或信息。 我尝试使用一个获取图像属性的新类,我只添加一个,但我不知道如何做到这一点。

我也不能使用已经在使用的属性“标签”,并且不能被覆盖

public partial class myImage : System.Windows.Controls.Image
{
        //? 
}

【问题讨论】:

  • 你说的是依赖属性,还是你真的不知道如何在 C# 类声明中编写属性?

标签: c# wpf image


【解决方案1】:

向从 Image 派生的类添加属性如下所示:

public class MyImage : Image
{
    public string AdditionalInfoProperty { get; set; }
}

您还可以创建一个包含ImageSource 对象的属性:

public class MyImage
{
    public ImageSource Image { get; set; }
    public string AdditionalInfoProperty { get; set; }
}

然后在您的 xaml 中,您可以像这样使用它,假设 DataContext 拥有一个 MyImage 类型的对象。

<Image Source="{Binding Image}"/>
<TextBlock Text="{Binding AdditionalInfoProperty}"/>

【讨论】:

  • System.Windows.Controls.Image 无法分配给 Image.Source 属性。它应该是 ImageSource,你应该解释一下区别。请说出您认为 INPC 到底是什么(当您只有自动实现的属性时,它可能有什么用途)。
  • 你说得对@Clemens,我有点懒。感谢您的意见。
  • 哇!那里容易。答案的重点是展示实现属性的方式以及如何将它们用作解决方案。我承认信息不是 100% 正确,但没有必要苛求。
  • 当然可以,但它可以很容易地改正。我会为你做的。
【解决方案2】:

获取或设置一个代表图像作者的值。

public System.Collections.ObjectModel.ReadOnlyCollection<string> Author { get; set; }

以下代码示例展示了如何使用 BitmapMetadata 类的友好属性将元数据写入位图图像。

FileStream stream3 = new FileStream( "image2.tif", FileMode.Create );
BitmapMetadata myBitmapMetadata = new BitmapMetadata( "tiff" );
TiffBitmapEncoder encoder3 = new TiffBitmapEncoder();
myBitmapMetadata.ApplicationName = "Microsoft Digital Image Suite 10";
myBitmapMetadata.Author = new ReadOnlyCollection<string>( 
    new List<string>() { "Mehdi Daustany" } );
myBitmapMetadata.CameraManufacturer = "Tailspin Toys";
myBitmapMetadata.CameraModel = "TT23";
myBitmapMetadata.Comment = "Nice Picture";
myBitmapMetadata.Copyright = "2010";
myBitmapMetadata.DateTaken = "5/23/2010";
myBitmapMetadata.Keywords = new ReadOnlyCollection<string>( 
    new List<string>() { "Mehdi", "Daustany" } );
myBitmapMetadata.Rating = 5;
myBitmapMetadata.Subject = "Mehdi";
myBitmapMetadata.Title = "Mehdi's photo";

// Create a new frame that is identical to the one 
// from the original image, except for the new metadata. 
encoder3.Frames.Add( 
    BitmapFrame.Create( 
    decoder2.Frames[0], 
    decoder2.Frames[0].Thumbnail, 
    myBitmapMetadata, 
    decoder2.Frames[0].ColorContexts ) );

encoder3.Save( stream3 );
stream3.Close();

【讨论】:

    【解决方案3】:

    假设你有一张图片

    Image img = Image.FromFile("C:\\temp\\smth\\smth.jpg");
    

    使用系统绘图获取属性项

    System.Drawing.Imaging.PropertyItem prop = img.PropertyItems[0];
    

    设置信息

    SetProperty(ref prop, 33432, "Test Info...");
    img.SetPropertyItem(prop);
    

    【讨论】:

    • 问题是关于WPF的。
    • 所以我正在使用数据库,我想在图像中添加一个 int (Person_ID),这样我就可以直接查看哪个图像属于哪个人,而无需再次使用数据库
    • 当您保存文件时,您可以将 Id 添加到文件的路径中,这是一种方法,然后您可以使用 SetProperty attr 添加属性项的 id 之一
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    相关资源
    最近更新 更多