tags:

views:

1496

answers:

4

Hello,

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

A: 

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.

Mike
+1  A: 

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

CraigD
Your JpegParser.cs is using System.Windows.Media.Imaging, it references the PresentationCore assembly. Doesn't that mean that it won't work on .NET framework 2.0?
Liam
Ah sorry, I mixed up two different projects there. The TagUpdater code is 3.5; but is based on this Searcharoo class which is definitely 2.0-compatiblehttp://searcharoo.codeplex.com/SourceControl/changeset/view/20374#490868... I posted the link because it was a small, self-contained project but forgot about the WPF inclusion. Turns out the System.Windows.Media.Imaging and BitmapImage reference can be removed anyway - half-done I guess. Will update my post too.
CraigD
+2  A: 

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.

CraigD
A: 

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
Liam