Gitz
Gitz

Reputation: 820

Image metadata (EXIF) with c#

I am using the following code and the code is working for some images is fine but most images EXIF data is not getting.

ImageMetadata imageMetadata = new ImageMetadata();
public ImageMetadata ReadEXIFMetadata(string filepath)
{
    FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
    System.Drawing.Image image__1 = System.Drawing.Image.FromStream(fs);
    PropertyItem[] imagePropertyItems = image__1.PropertyItems;

    foreach (PropertyItem pi in imagePropertyItems)
    {
        switch ((EXIFProperty)pi.Id)
        {
           case EXIFProperty.Title:
                imageMetadata.Title = Encoding.Unicode.GetString(pi.Value);
                break;
           case EXIFProperty.Author:
                imageMetadata.Author = Encoding.Unicode.GetString(pi.Value);
                //imageMetadata.Author = Encoding.UTF8.GetString(pi.Value)
                break;
           case EXIFProperty.Keywords:
                imageMetadata.Keywords = Encoding.Unicode.GetString(pi.Value);
                break;
           case EXIFProperty.Comments:
                imageMetadata.Comments = Encoding.Unicode.GetString(pi.Value);
                //imageMetadata.Comments = Encoding.UTF8.GetString(pi.Value)
                break;
           default:
                break;
        }
    }
    fs.Close();    
    return imageMetadata;
}


public enum EXIFProperty
{
    Title = 40091,
    Author = 40093,
    Keywords = 40094,
    Comments = 40092
}
public class ImageMetadata
{
    private string _title = string.Empty;
    private string _author = string.Empty;
    private string _keywords = string.Empty;
    private string _comments = string.Empty;
    public ImageMetadata()
    {
        this._title = string.Empty;
        this._author = string.Empty;
        this._keywords = string.Empty;
        this._comments = string.Empty;
    }
    public ImageMetadata(string title, string author, string keywords, string comments)
    {
        this._title = title;
        this._author = author;
        this._keywords = keywords;
        this._comments = comments;
    }
    public string Title
    {
        get
        {
            return this._title;
        }
        set
        {
            this._title = value;
        }
    }
    public string Author
    {
        get
        {
            return this._author;
        }
        set
        {
            this._author = value;
        }
    }
    public string Keywords
    {
        get
        {
            return this._keywords;
        }
        set
        {
            this._keywords = value;
        }
    }
    public string Comments
    {
        get
        {
            return this._comments;
        }
        set
        {
            this._comments = value;
        }
    }
}

Correct me if I am doing something wrong in the above code.please help stuck with this problem.

Upvotes: 0

Views: 4840

Answers (1)

JKennedy
JKennedy

Reputation: 18799

I'm not sure why you've created your own ImageMetaData class as .NET already has one. Try:

BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
BitmapMetadata importedMetaData = new BitmapMetadata("jpg");
using (Stream sourceStream = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
    BitmapDecoder sourceDecoder = BitmapDecoder.Create(sourceStream, createOptions, BitmapCacheOption.Default);
    // Check source is has valid frames 
    if (sourceDecoder.Frames[0] != null && sourceDecoder.Frames[0].Metadata != null)
    {
        sourceDecoder.Frames[0].Metadata.Freeze();
        // Get a clone copy of the metadata
        BitmapMetadata sourceMetadata = sourceDecoder.Frames[0].Metadata.Clone() as BitmapMetadata;
        importedMetaData = sourceMetadata;
    }
}
return importedMetaData;

Upvotes: 2

Related Questions