Voprosec Voprosec
Voprosec Voprosec

Reputation: 147

Value of image property (C#)

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

Upvotes: 2

Views: 4903

Answers (2)

Stan R.
Stan R.

Reputation: 16065

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

Upvotes: 5

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

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.

Upvotes: 1

Related Questions