图像属性的值(C#)
我正在尝试解决为Bitmap对象更改值ImageDescription的问题。 添加文件的描述。 搜索相关主题,我还没有找到解决方案。
我的代码:
public Bitmap ImageWithComment(Bitmap image) { string filePath = @"C:\1.jpg"; var data = Encoding.UTF8.GetBytes("my comment"); var propItem = image.PropertyItems.FirstOrDefault(); propItem.Type = 2; propItem.Id = 40092; propItem.Len = data.Length; propItem.Value = data; image.SetPropertyItem(propItem); image.Save(filePath); return image; }
但带有新评论的图片不保存在文件夹中((请帮帮我
根据MSDN – 属性标记,您必须为Id
使用正确的int值
样品
using (var image = new Bitmap(@"C:\Desert.jpg")) { string filePath = @"C:\Desertcopy.jpg"; var data = Encoding.UTF8.GetBytes("my comment"); var propItem = image.PropertyItems.FirstOrDefault(); propItem.Type = 2; propItem.Id = 0x010E; // <-- Image Description propItem.Len = data.Length; propItem.Value = data; image.SetPropertyItem(propItem); image.Save(filePath); }
使用MSDN中的以下号码
运行代码后,您可以看到它如何影响图像
之前
后
ID为40092转换为0x9C9C。 据此, 这不是有效的属性项ID。 根据这个 ,
如果图像格式支持属性项但不支持您尝试设置的特定属性,则此方法将忽略该尝试但不会引发exception。
从它的外观来看,您希望您的ID为0x010E。 另外,请参阅此处了解每个属性项ID的详细信息。