Why does System.Drawing.Image.GetPropertyItem behave differently on Windows XP / 2003 compared to Windows 7

StackOverflow https://stackoverflow.com/questions/19660123

  •  01-07-2022
  •  | 
  •  

Question

I am trying to read the orientation Exif value using System.Drawing.Image.GetPropertyItem(0x0112).

This works fine on Windows 7 and Windows 2008 R2, but fails on Windows XP / Windows Server 2003 (Get a 'Property Cannot Be Found' error).

On Windows XP / 2003, if I iterate over the available properties (using the PropertyItems property of System.Drawing.Image), I only get 0x5090 (PropertyTagLuminanceTable) and 0x5091 (PropertyTagChrominanceTable)

With the exact same image in Windows 7 / Windows 2008, I get 18 properties, one of which is 0x0112.

I suspect this may be related to the fact that Vista and later versions of Windows have a newer version of GDI+. Is there any way to get this to work on Windows XP / 2003 without having to write code to read and write Exif data the hard way?

Was it helpful?

Solution

There is a library on NuGet called ExifReader and an accompanying CodeProject article here.

The following code should work for getting the orientation, and you can use the other enumerations of ExifTags to get other pieces of metadata:

object result;

var reader = new ExifReader("c:\\temp\\test\\sample.jpg");
reader.GetTagValue(ExifTags.Orientation, out result);

var orientation = Convert.ToInt32(result);

I have tested this with this image on Windows XP 32-Bit SP3 using .Net 4, and it returned an orientation of 1, which appears to be correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top