Extract image file metadata

I want to be able to extract image metadata and advanced properties without opening a file. In other words, how can I programmatically access the information displayed when I right-click on a file in the windows and select the Details tab?

Screen shot

What is the correct way to do this with .Net Framework 4.5 and higher? I found various ways to do this, but all of the articles are related to .Net Framework 2 and are at least tedious and probably outdated. In particular, are there any libraries that make this easy to do?

EDIT

I need to get height and width dimensions for images in the following formats: png, jpg, jpeg, gif, bmp and pdf (we can safely assume that PDF consists of only 1 page).

+6
source share
3 answers

I played to accomplish this. Below are my findings.

  • You can use the System.Drawing and System.Drawing.Imaging namespaces
  • The latter is imported using the operator and executes the Add reference => Assemblies => Framework => System.Drawing command
  • Find out what the values ​​of the hexadecimal values ​​that are stored in the PropertyItems Id value mean.
  • Convert them to human-readable text with an ASCII converter. *
  • Print on the screen or something else.

* Please note: the ASCII converter prints a bunch of strange characters (for example, "♦ ☺ ☺ ☻ ♥ ♣ ☺ ☺ ☺ ☻ ☺ ☺ ☻ ♥ ♣"), and my laptop starts to sound some properties. You probably need a different encoding for them. If anyone knows that, 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.:-)

/ Editing as a sasha_gud comment using the BitmapSource class , which exposes EXIF ​​metadata through a metadata property , is perhaps easier.

+2
source

I did this personally myself, as I needed to read XMP data for one of my projects.

For this, I used: http://sourceforge.net/projects/csxmptk/ , which is a wrapper for Adobe XMP toolkit.

This is not the most productive, but it did not bother me, since I did it as a batch process when the asset was loaded.

+1
source

Of course, something should open the file for reading metadata. This may not be the most efficient way, but the System.Drawing.Image class should provide you with everything you need. Use FromFile to create an Image object. Then you have the Height and Width properties for the dimensions and PropertyIdList and PropertyItems to get other information that may be available.

By the way, as far as I know, height and width are available for each image format.

+1
source

Source: https://habr.com/ru/post/972884/


All Articles