How do I get string representation of jpeg EXIF tags in c#?

-1

There are a lot of examples how you can enumerate and edit EXIF tags using Bitmap class.

Here's one:

        Bitmap bJpgWithExifData = (Bitmap)Image.FromFile(@"myfile.jpg");

        foreach (var item in bJpgWithExifData.PropertyItems)
        {
            Console.WriteLine( "{0}\t{1}\t{2}",item.Type,item.Id,item.Len);
        }

However, this doesn't explain how I get the string values of those tags. The data is binary, and when I try to decode it with Encoding.ASCII.GetString(item.Value), I get some junk instead of proper properties.

How do I get the string values?

UPDATE

Here are the exif tags as shown by JPEGSnoop:

  EXIF IFD0 @ Absolute 0x00000014
    Dir Length = 0x0010
    [Model                               ] = "GT-I9500"
    [Orientation                         ] = Row 0: top, Col 0: left
    [WhiteBalance                        ] = Auto white balance
    [DateTime                            ] = "2014:07:30 13:24:28"
    [Make                                ] = "SAMSUNG"
    [ExifOffset                          ] = @ 0x010C
    Offset to Next IFD = 0x00000000

When enumerating c# properties, I see 18 different properties, 2 of which are type 2 ("2014:07:30 13:24:28" and "SAMSUNG" converted to string)

How do I get the values of the rest?

UPDATE 1

Here's the code I use to print the values:

        foreach (var item in bJpgWithExifData.PropertyItems)
        {
            Console.WriteLine("{0}\t0x{1}\t{2}\t{3}\t{4}",item.Type,item.Id.ToString("x"),item.Len,Convert.ToBase64String(item.Value),Encoding.ASCII.GetString(item.Value));

        }

And here's the output:

3       0x100   2       IBA=     >
3       0x101   2       Egk=    ¦
2       0x10f   8       U0FNU1VORwA=    SAMSUNG
3       0x112   2       AQA=    O
2       0x132   20      MjAxNDowNzozMCAxMzoyNDoyOAA=    2014:07:30 13:24:28
10      0x829a  8       PgAAAOgDAAA=    >   ?¦
10      0x829d  8       FgAAAAoAAAA=    -

4       0x201   4       KgEAAA==        *O
4       0x202   4       AAAAAA==
3       0x8822  2       AgA=    O
3       0x8827  2       MgA=    2
3       0x9207  2       AgA=    O
3       0x9208  2       AAA=
3       0x9209  2       AQA=    O
5       0x920a  8       pAEAAGQAAAA=    ?O  d
3       0xa403  2       AAA=
3       0x5091  128     BwAHAAoAEwAoACgAKAAoAAcACAAKABoAKAAoACgAKAAKAAoAFgAoACgAKAAoACgAEwAaACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAAoACgAKAA=
 ! ( ( ( (
 > ( ( ( (

 - ( ( ( ( ( ! > ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (
3       0x5090  128     BgAEAAQABgAKABAAFAAYAAUABQAGAAgACgAXABgAFgAGAAUABgAKABAAFwAcABYABgAHAAkADAAUACMAIAAZAAcACQAPABYAGwAsACkAHwAKAA4AFgAaACAAKgAtACUAFAAaAB8AIwApADAAMAAoAB0AJQAmACcALQAoACkAKAA=    ¦ ¦ ¦ ¦
 > ¶ ^ ¦ ¦
 ¦ ^ - ¦ ¦ ¦
 > ¦ L - ¦       + ¶ #   v       0 - < , ) Ў
 d - >   * - % ¶ > Ў # ) 0 0 ( - % & ' - ( ) (
c#
bitmap
jpeg
gdi+
exif
asked on Stack Overflow Jul 31, 2014 by Arsen Zahray • edited Jul 31, 2014 by Arsen Zahray

2 Answers

1

I had to do the same thing a while ago. There is a Type property on the PropertyItem object. You'll need to use that value to determine what you need to do with each type of object.

I found this on Code Project that did a majority of the work on how to piece together the different types of values and then modified just a very little bit in order to customize some of the results.

Unfortunately, it's a ludicrously intricate process that is far too lengthy to go into here, but the little overview I gave should give you a good enough idea to check out the project, or know where to start if that URL ever dies in the future.

answered on Stack Overflow Jul 31, 2014 by krillgar
1

User contributions licensed under CC BY-SA 3.0