Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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, as far as I can see, shows code like the following;

BitmapMetadata bmd = (BitmapMetadata)frame.Metadata;
string a1 = (string)bmd.GetQuery("/app1/ifd/exif:{uint=36867}");

But in my ignorance I have no idea what bit of metadata GetQuery() will return, or what to pass it.

I want to attempt reading XMP first, falling back to EXIF if XMP does not exist. Is there a simple way of doing this?

Thanks.

like image 599
tsvallender Avatar asked Feb 17 '10 13:02

tsvallender


People also ask

What is EXIF and XMP?

The "Extensible Metadata Platform" (XMP) is an ISO standard created and adopted by Adobe Systems Inc. XMP is different from EXIF ​​as it does not contain basic details about the image or the camera. Instead, XMP metadata helps in the processing and interchange of image files.

Is EXIF data the same as metadata?

What's the difference between metadata and EXIF? In digital photography, metadata is the information stored within an image describing the camera settings used, the shoot location, and more. An EXIF is the file that stores this metadata.

Can Metadata be stored in JPEG?

Metadata is stored in two main places: Internally – embedded in the image file, in formats such as JPEG, DNG, PNG, TIFF … Externally – outside the image file in a digital asset management system (DAM) or in a “sidecar” file (such as for XMP data) or an external news exchange format document as specified by the IPTC.


2 Answers

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;
        }
    }
like image 62
tsvallender Avatar answered Oct 06 '22 00:10

tsvallender


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

like image 36
Drew Noakes Avatar answered Oct 06 '22 01:10

Drew Noakes