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.
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.
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.
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.
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;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With