-
Notifications
You must be signed in to change notification settings - Fork 486
Getting Started (dotnet)
metadata-extractor
is available via NuGet,
or via a download from the releases page.
Using this library is straightforward. Conceptually there are two steps to its usage:
- Read
Directory
objects from an image - Query the
Directory
objects to retrieve one or more values
NOTE:
- These examples are based upon version 2 of the API. Earlier versions may differ slightly.
-
using
directives and exception handling code have been omitted for clarity. - Fully-featured sample classes are available in the source distribution.
The simplest way to read metadata is to use ImageMetadataReader
.
If you know the type of file you're reading from then you can replace ImageMetadataReader
with
a more specific reader, such as JpegMetadataReader
. Using ImageMetadataReader
is often safer
as it uses FileTypeDetector
to inspect the file's contents in order to determine which decoding
method to use and incurs very little overhead.
For an image file of any supported type, use:
var directories = ImageMetadataReader.ReadMetadata("myImage.jpg");
Reading from a Stream
is much the same:
var directories = ImageMetadataReader.ReadMetadata(stream);
After reading metadata, you'll have zero or more Directory
objects. These in turn contain zero or
more Tag
objects. Tags hold values representing the metadata of the source image.
foreach (var directory in directories)
{
foreach (var tag in directory.Tags)
{
Console.WriteLine(tag);
}
}
// obtain the Exif SubIFD directory
var directory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
if (directory != null)
{
// query the tag's value
if (directory.TryGetDateTime(ExifDirectoryBase.TagDateTimeOriginal, out var dateTime))
return dateTime;
}
Note that tag values have specific data types. Attempts will be made to convert to the type you request, but this is not always possible.
If you only wish to display a friendly string version of the tag's value, simply call Tag.ToString()
.
Alternatively, you can wrap the directory with a Descriptor
and have strongly-typed access with
descriptive method names:
// obtain a specific directory
var directory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
if (directory != null)
{
// create a descriptor
var descriptor = new ExifSubIfdDescriptor(directory);
// get tag description
String program = descriptor.GetExposureProgramDescription();
}
In the above example, program
would contain a detailed string such as "Manual control"
or
"Aperture Priority"
, whereas the raw tag value is actually an integer. Descriptors not only
decode enum values, but also prepend/append units ("-1 EV"
, "f/2.8"
) and provide methods that
calculate derived properties for convenience.