本文介绍了将图像描述保存在PNG文件元数据中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试使用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); 哪个引发异常. 解决方案 PropertyItem Class (System.Drawing.Imaging) | Microsoft Docs[^]: 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[^]. Image.GetPropertyItem(Int32) Method (System.Drawing) | Microsoft Docs[^]: 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文件元数据中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 00:23