4

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

  • Just to clarify - re the 3.0/3.5 - is this using WPF? – Marc Gravell Mar 25 '09 at 08:25
  • As far as I know the 3.0/3.5 are using the WPF for reading the IPTC information. And the WPF is _not_ avaiable for the 2.0. –  Mar 25 '09 at 08:36
  • Is there any general solution for using 3.0/3.5 features under 2.0? – Liam Apr 28 '09 at 08:50
  • 2
    Yes. Upgrade. There's no reason not to do it. .NET 3.0 and 3.5 are additive. They do not replace .NET 2.0. They simply apply two service packs - to .NET 2.0 SP2. You also don't need Visual Studio 2008 to upgrade. You can just upgrade the Framework. – John Saunders Apr 28 '09 at 11:42
  • Are there any risks or side effects to upgrading a Windows 2003 server which runs some custom software which is built on .NET 2.0? Can I just apply updates and restart? – Liam Apr 28 '09 at 16:29
  • This IPTC technical specification may be useful: https://www.iptc.org/std/IIM/4.1/specification/IIMV4.1.pdf – sj26 Dec 09 '15 at 22:55

6 Answers6

3

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.

Conceptdev
  • 3,261
  • 1
  • 21
  • 23
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).

Todd Price
  • 2,650
  • 1
  • 18
  • 26
1

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

DaNet
  • 398
  • 3
  • 6
  • 20
  • The Photoshop Segment is APP13, not APP14 like the article suggests. Parsing JPEG, Photoshop resources and IPTC records using regexps also doesn't seem like a great idea. – sj26 Dec 09 '15 at 22:48
1

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

Conceptdev
  • 3,261
  • 1
  • 21
  • 23
  • 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 Apr 28 '09 at 08:57
  • 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-compatible http://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. – Conceptdev Apr 28 '09 at 10:57
0

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.

Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
0

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
  • 19,819
  • 24
  • 83
  • 123