using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
namespace ImageMetadata
{
class Program
{
static void Main(string[] args)
{
string imagePath = "example.jpg";
string newPath = "example_with_metadata.jpg";
Image image = Image.FromFile(imagePath);
PropertyItem[] propItems = image.PropertyItems;
PropertyItem propItem = (PropertyItem)Activator.CreateInstance(typeof(PropertyItem));
propItem.Id = 0x010E;
propItem.Type = 2;
propItem.Value = Encoding.ASCII.GetBytes("This is a sample image description.");
propItem.Len = propItem.Value.Length;
image.SetPropertyItem(propItem);
image.Save(newPath, ImageFormat.Jpeg);
Image newImage = Image.FromFile(newPath);
foreach (PropertyItem item in newImage.PropertyItems)
{
if (item.Id == 0x010E)
{
Console.WriteLine("Image Description: " + Encoding.ASCII.GetString(item.Value));
}
}
Console.WriteLine("Metadata added and read successfully.");
}
}
}