I want to be able to extract an image's metadata and extended properties without opening the file. In other words, how do I programatically access the information shown when you right-click a file in windows and select the "Details" tab?
What is the correct way to do this using .Net Framework 4.5 and higher? I have found various ways of doing this, but the articles all refer to .Net Framework 2 and is, to say the least, tedious and probably outdated. Specifically, are there any libraries which makes this easy to do?
I need to be able to get the height and width dimensions for images in the following formats: png, jpg, jpeg, gif, bmp and pdf (it is safe to assume that the pdf consists of 1 page only).
I was playing around to accomplish this. Below are my findings.
PropertyItems
Id
value.*Please note: ASCII converter prints a bunch of strange characters (e.g., "♦ ☺ ☺ ☻ ♥ ♣ ☺ ☺ ☺ ☻ ☺ ☺ ☻ ♥ ♣ ☺ ☻ ♥ ♦ ♀ ☻ ♥ ♣") and my laptop starts beeping with some properties. You probably need other encoding for those. If anyone knows which, let me know.
using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// Add reference => Assemblies => Framework => System.Drawing
// Set the path to your photo file.
var path = @"[Path to your image]";
// Create image object
var image = new Bitmap(path);
// Get PropertyItems
var propItems = image.PropertyItems;
// Remove encoding object
var encodings = new ASCIIEncoding();
// Display
foreach (var propItem in propItems)
{
// The values below come from http://msdn.microsoft.com/en-us/library/xddt0dz7(v=vs.110).aspx
// Find al values available with Console.WriteLine(propItem.Id.ToString("x"));
if (propItem.Id.ToString("x").Equals("110")) //Equipment model
{
var model = encodings.GetString(propItem.Value);
Console.WriteLine("Model:\t" + model);
}
if (propItem.Id.ToString("x").Equals("9003")) //ExifDTOriginal
{
var datetime = encodings.GetString(propItem.Value);
Console.WriteLine("Taken:\t" + datetime);
}
}
Console.Read();
}
}
}
// =>
// Model: FinePix S9500
// Taken: 2012:11:30 13:58:04
Hope this helps. :-)
/Edit as a comment by sasha_gud states, using the BitmapSource class, which exposes the EXIF metadata through the Metadata property is probably easier.
Of course, something needs to open the file to read the metadata. That being said, it may not be the most efficient way but the System.Drawing.Image
class should give you everything you need. Use FromFile
to create the Image object. You then have Height
and Width
properties for the dimensions and PropertyIdList
and PropertyItems
to get other information that may be available.
BTW, as far as I know, height and width are available for every image format.
I have done this personally myself as I was required to read XMP data for one of my projects.
To do this I used: http://sourceforge.net/projects/csxmptk/ which is a wrapper around the Adobe's XMP toolkit.
It's not the most performant but this was not a concern for me as I did this as a batch process when the asset was uploaded.
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