Reading data metadata from JPEG, XMP or EXIF in C#

前端 未结 5 1961
悲&欢浪女
悲&欢浪女 2020-12-14 18:56

I\'ve been looking around for a decent way of reading metadata (specifically, the date taken) from JPEG files in C#, and am coming up a little short. Existing information, a

相关标签:
5条回答
  • 2020-12-14 19:32

    I think what you are doing is a good solution because the System.DateTaken handler automatically apply Photo metadata policies of falling back to other namespaces to find if a value exist.

    0 讨论(0)
  • 2020-12-14 19:34

    The following seems to work nicely, but if there's something bad about it, I'd appreciate any comments.

        public string GetDate(FileInfo f)
        {
            using(FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BitmapSource img = BitmapFrame.Create(fs);
                BitmapMetadata md = (BitmapMetadata)img.Metadata;
                string date = md.DateTaken;
                Console.WriteLine(date);
                return date;
            }
        }
    
    0 讨论(0)
  • 2020-12-14 19:45

    If you're struggling with XMP jn jpeg, this works. It's not called brutal for nothing!

    public class BrutalXmp
    {
        public XmlDocument ExtractXmp(byte[] jpegBytes)
        {
            var asString = Encoding.UTF8.GetString(jpegBytes);
            var start = asString.IndexOf("<x:xmpmeta");
            var end = asString.IndexOf("</x:xmpmeta>") + 12;
            if (start == -1 || end == -1)
                return null;
            var justTheMeta = asString.Substring(start, end - start);
            var returnVal = new XmlDocument();
            returnVal.LoadXml(justTheMeta);
            return returnVal;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 19:48

    I've ported my long-time open-source Java library to .NET recently, and it supports XMP, Exif, ICC, JFIF and many more types of metadata across a range of image formats. It will definitely achieve what you're after.

    https://github.com/drewnoakes/metadata-extractor-dotnet

    var directories = ImageMetadataReader.ReadMetadata(imagePath);
    var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
    var dateTime = subIfdDirectory?.GetDescription(ExifDirectoryBase.TagDateTime);
    

    This library also supports XMP data, via a C# port of Adobe's XmpCore library for Java.

    https://github.com/drewnoakes/xmp-core-dotnet

    0 讨论(0)
  • 2020-12-14 19:50

    My company makes a .NET toolkit that includes XMP and EXIF parsers.

    The typical process is something like this:

    XmpParser parser = new XmpParser();
    System.Xml.XmlDocument xml = (System.Xml.XmlDocument)parser.ParseFromImage(stream, frameIndex);
    

    for EXIF you would do this:

    ExitParser parser = new ExifParser();
    ExifCollection exif = parser.ParseFromImage(stream, frameIndex);
    

    obviously, frameIndex would be 0 for JPEG.

    0 讨论(0)
提交回复
热议问题