Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract image file metadata

Tags:

c#

windows

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?

Screenshot

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?

EDIT

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).

like image 242
CShark Avatar asked Jul 26 '14 12:07

CShark


3 Answers

I was playing around to accomplish this. Below are my findings.

  1. You can use System.Drawing and System.Drawing.Imaging namespace
  2. The latter is imported by using statement and doing Add reference => Assemblies => Framework => System.Drawing
  3. Find out what the hexadecimal values mean that are stored in PropertyItems Id value.
  4. Convert them to human readable text with ASCII converter.*
  5. Print to screen, or whatever.

*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.

like image 86
Erwin Rooijakkers Avatar answered Oct 04 '22 23:10

Erwin Rooijakkers


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.

like image 41
Jim Rhodes Avatar answered Oct 04 '22 21:10

Jim Rhodes


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.

like image 36
Christopher Cartlidge Avatar answered Oct 04 '22 21:10

Christopher Cartlidge