0

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 +エラーが一般的に発生しました。」

私が理解するのを手伝ってください!私が間違っていることは何ですか?

4

1 に答える 1

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);
        }
    }
}
于 2013-01-12T00:10:05.693 に答える