Tutorial :Reading IPTC information with C#, .NET Framework 2



Question:

is there any possibility to read the IPTC information of a picture with C# and the .NET Framework 2?

I haven't found any solution. Only with .NET Framework 3.0 oder .NET 3.5 you can do it.

Any help, any information?

Thank you very much from Germany! Stephan


Solution:1

OK, my previous answer was a little 'confused'. Here is a link to a .NET 2.0 project (Visual Studio 2008 SLN format) that provides some basic 'extraction' functionality MetaExtractor ZIP (25Kb)

CODE SNIP:

// The Parser class extracts the data to hardcoded properties.  // it's 1200 lines - too many to post on StackOverflow  JpegParser parser = new JpegParser(path);  if (parser.ParseDocument())  {      Console.WriteLine("Parsed {0} {1}", System.IO.Path.GetFileName(path), parser.Title);      Console.WriteLine("Tags: {0}", parser.KeywordString);      Console.WriteLine("Description: {0}", parser.Description);      Console.WriteLine("Title: {0}", parser.Title);      Console.WriteLine("Rating: {0}", parser.Rating);  }  

USAGE:

MetaExtractor "C:\Users\Craig\Pictures\anton-1.jpg"  

OUTPUT:

 == DeepZoomPublisher MetaExtractor v0.1 ==  Parsed anton-1.jpg Beach Photo  Tags: beach, blue sky  Description: Anton  Title: Beach Photo  Rating: 3    Press any key to exit...  

Hope that helps more than my previous answer.


Solution:2

I realize this answer does not address .NET framework v2, but I thought it was worth documenting for those of you who are using .NET 3.5 or higher. This might also work in 3.0 but I have not tested it there.

The following function call will return the keywords embedded in a JPEG image:

    private string[] GetKeywords(string filespec)      {          BitmapDecoder decoder = new JpegBitmapDecoder(new FileStream(filespec, FileMode.Open), BitmapCreateOptions.None, BitmapCacheOption.None);          BitmapMetadata meta = (BitmapMetadata)decoder.Frames[0].Metadata;          return meta.Keywords.ToArray<string>();      }  

The BitmapDecoder and BitmapMetadata class are contained in an assembly that is normally used in WPF, so you'll need to reference the following assemblies to use those classes:

  • PresentationCore
  • WindowsBase

I am successfully using this approach in a WinForm app, but I suspect it could be adapted for other application types. Also, you can see that the abstract "BitmapDecoder" class in this example is assigned a JpegBitmapDecoder instance, but you could get an instance of another decoder for your image type (TIFF, GIF, PNG, BMP, and WMP are also supported).


Solution:3

Stephan,

These two links should be useful

Reading XMP metadata from JPEG

EXIF extractor (on CodeProject)

They both access slightly different parts of the JPEG header to extract the various metadata that can be embedded. I have used their code in Searcharoo (which you can download) and to extract the lat/long from JPEGs for this DeepZoom example.

You can grab my JpegParser.cs class from this 13kb code ZIP - it only grabs a couple of properties (Title/Description/Keywords/Rating/Latitude-Longitude) but you should be able to see in the code where to extract more == SEE EDIT BELOW ==

NOTE: the hard work was all done by the authors of the two articles linked above.

EDIT: comment below highlight the face that the JpegParser.cs I referenced above includes a reference to using System.Windows.Media.Imaging; and BitmapImage img = new BitmapImage(new Uri(filename));. These were added as part of an (unfinished) enhancement, so they can be safely removed and the JpegParser.cs class should then run in 2.0 (although the containing project will not - sorry for the confusion).

Alternatively, you can get similar code (some editing will be required) from JpegDocument.cs class in Searcharoo - a .NET 2.0 application that indexes files (including JPEGs) for example this search result


Solution:4

I have just searched almost the entire web to find a C# solution to extract IPTC information, and found this nice and brand new tutorial at Code Project:

http://www.codeproject.com/KB/graphics/ReadingIPTCAPP14.aspx

Hope it helps someone. :)


Solution:5

If it was implemented in 3.x, it was not present in earlier versions.

However, there are 3rd party libraries that can do the trick. ImageMagick is one of them. If you are looking for a simpler (and free) implementation, this article or a google search may lead you to a solution.

Best of luck.


Solution:6

Having tried several suggestions from here and elsewhere with no luck, I have settled on writing a class to call out to the exiv2 command-line tool. A small performance penalty for spawning a process for each image is acceptable in my scenario, it may not be in others.

  • Call exiv2.exe using System.Process
  • Pass arguments "-pi filename.jpg" to output all IPTC fields
  • Read output using System.Process.StandardOutput.ReadToEnd();
  • The output can be split into its parts using Regex

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »