我正在尝试解决为Bitmap对象更改值PropertyTagImageDescription(0x010E)的问题。添加文件的描述。搜索相关主题,并没有找到解决方案。我的用途:
Bitmap image = new Bitmap(Image.FromFile(fileName));
var data = System.Text.Encoding.UTF8.GetBytes("My comment");
PropertyItem propItem = image.GetPropertyItem(Convert.ToInt32(0x010E));
propItem.Len = data.Length;
propItem.Value = data;
image.SetPropertyItem(propItem);
但是有一个错误:“在GDI +错误中发生了通用。”
帮帮我理解!我做错了什么?
答案 0 :(得分:1)
我无法找到您的错误我没有找到具有0x010E属性设置的图像。但我已经构建了一个有效的小控制台应用程序:
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string imageLocation = @"C:\Users\Jens\Desktop\image.jpg";
string newImageLocation = @"C:\Users\Jens\Desktop\newImage.jpg";
// http://msdn.microsoft.com/en-us/library/ms534415(VS.85).aspx
Int32 ImageDescription = 0x010E;
// get file stream and create Image
using (var fs = new FileStream(imageLocation, FileMode.Open, FileAccess.ReadWrite))
using (var img = Image.FromStream(fs, false, false))
{
var data = Encoding.UTF8.GetBytes("My comment");
// get a property from the image file and use it as container
var propItem = img.PropertyItems.FirstOrDefault();
// set the values that u like to add
// http://msdn.microsoft.com/en-us/library/system.drawing.imaging.propertyitem.aspx
propItem.Type = 2;
propItem.Id = ImageDescription;
propItem.Len = data.Length;
propItem.Value = data;
// add property to Image and save it to the system
img.SetPropertyItem(propItem);
img.Save(newImageLocation);
}
}
}