将图像描述保存在PNG文件元数据中 [英] Save image description in PNG file meta data

查看:138
本文介绍了将图像描述保存在PNG文件元数据中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PropertyItem将图像描述保存在PNG文件中。它没有给我任何错误,图像成功保存。但是,当读回它时,它似乎不包含图像描述。在GIMP中打开文件也不会显示图像描述。



我做错了什么?



我尝试过:



我保存PNG的代码是:

 using(var bitmap = TakePicture ()){
var imageDescription =此图像的描述;
var propertyItem =(PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
propertyItem.Id = 0x010E; // 图片描述。请参阅https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id(v=vs.110).aspx
propertyItem.Type = 1; //一个字节数组。请参阅https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.type(v=vs.110).aspx
propertyItem.Value = Encoding.ASCII.GetBytes(imageDescription );
propertyItem.Len = propertyItem.Value.Length;
bitmap.SetPropertyItem(propertyItem);

bitmap.Save(filename);
}





我读回的代码是:

 var bitmap =(Bitmap)Image。 FROMFILE(文件名); 
var propertyItem = bitmap.GetPropertyItem(0x010E);
var imageDescription = Encoding.ASCII.GetString(propertyItem.Value);



哪个引发异常 System.ArgumentException:找不到属性。< /var>.

解决方案

A PropertyItem 对象用于检索和更改现有图像文件的元数据,不创建元数据。因此, PropertyItem 类没有已定义的Public构造函数,并且您无法创建 PropertyItem 对象的实例。



要解决缺少Public构造函数的问题,请使用现有的 PropertyItem 对象,而不是创建新实例 PropertyItem 类。有关详细信息,请参阅 Image.GetPropertyItem [ ^ ]。

< blockquote class =quote>

设置属性项很困难,因为PropertyItem类没有公共构造函数。解决此限制的一种方法是通过检索PropertyItems属性值或调用已具有属性项的Image的GetPropertyItem方法来获取PropertyItem。然后你可以设置PropertyItem的字段并将其传递给SetPropertyItem。



文档似乎暗示你不能设置属性,除非它们首先存在。



WPF成像库似乎支持它:

如何:将元数据写入位图Microsoft Docs [ ^ ]



或者您可以使用替代库 - 例如:

GitHub - mono / taglib-sharp:用于在媒体文件中读取和写入元数据的图书馆 [ ^ ]


I'm trying to save an image description in a PNG file using a PropertyItem. It's not giving me any errors and the image is successfully saved. When reading it back however, it doesn't appear to contain the image description. Opening the file in GIMP doesn't show the image description either.

What am I doing wrong?

What I have tried:

My code for saving the PNG is:

using (var bitmap = TakePicture()) {
	var imageDescription = "Description of this image";
	var propertyItem = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
	propertyItem.Id = 0x010E;     // Image description. See https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.id(v=vs.110).aspx
	propertyItem.Type = 1;        // A byte array. See https://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.type(v=vs.110).aspx
	propertyItem.Value = Encoding.ASCII.GetBytes(imageDescription);
	propertyItem.Len = propertyItem.Value.Length;
	bitmap.SetPropertyItem(propertyItem);

	bitmap.Save(filename);
}



My code for reading it back is:

var bitmap = (Bitmap)Image.FromFile(filename);
var propertyItem = bitmap.GetPropertyItem(0x010E);
var imageDescription = Encoding.ASCII.GetString(propertyItem.Value);


Which throws an exception System.ArgumentException: Property cannot be found..

解决方案

A PropertyItem object is used to retrieve and to change the metadata of existing image files, not to create the metadata. Therefore, the PropertyItem class does not have a defined Public constructor, and you cannot create an instance of a PropertyItem object.

To work around the absence of a Public constructor, use an existing PropertyItem object instead of creating a new instance of the PropertyItem class. For more information, see Image.GetPropertyItem[^].

It is difficult to set property items, because the PropertyItem class has no public constructors. One way to work around this restriction is to obtain a PropertyItem by retrieving the PropertyItems property value or calling the GetPropertyItem method of an Image that already has property items. Then you can set the fields of the PropertyItem and pass it to SetPropertyItem.


The documentation seems to imply that you can't set properties unless they exist first.

The WPF imaging libraries seem to support it:
How to: Write Metadata to a Bitmap | Microsoft Docs[^]

Or you could use an alternative library - for example:
GitHub - mono/taglib-sharp: LIbrary for reading and writing metadata in media files[^]


这篇关于将图像描述保存在PNG文件元数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆