Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改PropertyTagImageDescription的值_C#_Image_Bitmap - Fatal编程技术网

C# 更改PropertyTagImageDescription的值

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

我试图解决更改位图对象的值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+中发生了一般错误。”


帮我理解!我做错了什么?

我无法找到您的错误,我没有找到设置了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);
        }
    }
}

我需要更改文件注释!不出口