2

I'm trying to solve the problem of changing the value ImageDescription for the Bitmap object. To add a description for the file. Searching the related topics, I have not found a solution.

My code:

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;
}

But image with new comment dont save in folder(( Please help me

Brian
  • 5,069
  • 7
  • 37
  • 47

2 Answers2

5

According to MSDN - Property Tags you have to use the proper int value for the Id

Sample

 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);
            }

using the following number from MSDN

image description code

after running the code, you can see how it affected the image

Before

original

After

after edit

Stan R.
  • 15,757
  • 4
  • 50
  • 58
1

An ID of 40092 translates to 0x9C9C. According to this, that's not a valid property item id. According to this,

If the image format supports property items but does not support the particular property you are attempting to set, this method ignores the attempt but does not throw an exception.

From the looks of it, you want your ID to be 0x010E. Also, see here for details on each property item id.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76