Search code examples
c#asp.netmetadatajpegexif

How do I determine the Id for the title in jpeg when using System.Drawing.Image


So far I created a simple upload function, download function and the a class to change file properties, but right now how do I determine the Id for the attributes or properties I want to change eg. Title, Author, Subject, etc. Currently I'm using System.Drawing.Imaging.dll for my library.

This is how far I got with my ChangeFileProperties class:

public class ChangeFileProperties
{ 
    public void ChangeFileAuthor(string FilePath)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath);

        //??
        PropertyItem propitem = image.GetPropertyItem(0x0320);
        //??
        propitem.Id = 0x0321;
        propitem.Len = propitem.Value.Length;
        propitem.Type = 1;
        propitem.Value = Encoding.UTF8.GetBytes("MyImageInfo");
        image.SetPropertyItem(propitem);


        image.Save(Environment.CurrentDirectory + @"\test.jpeg");
    }
}

So right now, there no error in the codes but when I run the webpage, the errors pop up on the ?? comment part, the errors said

An exception of type 'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code

Additional information: Property cannot be found.

It seems the Id is wrong, so how do I know what Id is it for each attributes.


Solution

  • If you use the Image.PropertyItems collection, you can get all the properties and loop through them:

    var image = System.Drawing.Image.FromFile(@"C:\Users\wired\Pictures\1496422850103.jpeg");
    
    foreach (var prop in image.PropertyItems)
    {
        var id = prop.Id;
        //etc
    }