繁体   English   中英

使用C#的图像元数据(EXIF)

[英]Image metadata (EXIF) with c#

我正在使用以下代码,该代码对某些图像有效,但大多数图像EXIF数据均未获取。

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

如果我在上面的代码中做错了,请纠正我。请帮助解决此问题。

我不确定为什么要创建自己的ImageMetaData类,因为.NET已经有一个。 尝试:

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM