C# 更改PropertyTagImageDescription的值
我试图解决更改位图对象的值PropertyTagImageDescription(0x010E)的问题。为文件添加说明。正在搜索相关主题,但未找到解决方案。我的用途:C# 更改PropertyTagImageDescription的值,c#,image,bitmap,C#,Image,Bitmap,我试图解决更改位图对象的值PropertyTagImageDescription(0x010E)的问题。为文件添加说明。正在搜索相关主题,但未找到解决方案。我的用途: Bitmap image = new Bitmap(Image.FromFile(fileName)); var data = System.Text.Encoding.UTF8.GetBytes("My comment"); PropertyItem propItem = image.GetPropertyItem(Conver
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+中发生了一般错误。”
帮我理解!我做错了什么?我无法找到您的错误,我没有找到设置了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);
}
}
}
我需要更改文件注释!不出口