oozcitak / exiflibrary

A .Net Standard library for editing Exif metadata
MIT License
131 stars 48 forks source link

Exception in ReadExifAPP1() due to duplicate keys when reading one particular image #80

Closed davidebbo closed 3 years ago

davidebbo commented 3 years ago

I have one jpeg image (apparently from some Windows Phone) where I get this exception when calling ImageFile.FromFile():

System.ArgumentException
  HResult=0x80070057
  Message=An item with the same key has already been added. Key: 384 (Parameter 'key')
  Source=System.Collections
  StackTrace:
   at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value)
   at ExifLibrary.JPEGFile.ReadExifAPP1() in D:\Code\Github\exiflibrary\ExifLibrary\JPEGFile.cs:line 624
   at ExifLibrary.JPEGFile..ctor(MemoryStream stream, Encoding encoding, Boolean readTrailingData) in D:\Code\Github\exiflibrary\ExifLibrary\JPEGFile.cs:line 176
   at ExifLibrary.ImageFile.FromStreamInternal(MemoryStream stream, Encoding encoding) in D:\Code\Github\exiflibrary\ExifLibrary\ImageFile.cs:line 278
   at ExifLibrary.ImageFile.FromStream(Stream stream, Encoding encoding) in D:\Code\Github\exiflibrary\ExifLibrary\ImageFile.cs:line 175
   at ExifLibrary.ImageFile.FromFile(String filename, Encoding encoding) in D:\Code\Github\exiflibrary\ExifLibrary\ImageFile.cs:line 142
   at ExifLibrary.ImageFile.FromFile(String filename) in D:\Code\Github\exiflibrary\ExifLibrary\ImageFile.cs:line 129
   ...

I'm not familiar enough with the jpeg format to make sense of the code, but what I observe is that at the time of the exception, ifdqueue has 2 entries:

[0] -> [384, GPS] [1] -> [562, First]

And then we try to insert a [384, Interop], which blows up because it already has the key 384.

If duplicates are expected, maybe the solution is to use a custom comparer that allows it, as in https://stackoverflow.com/a/21886340. Just a guess!

Awesome library by the way, I've used it successfully on many images!

davidebbo commented 3 years ago

I tried the comparer implementation that allows duplicates from that SO question, and it makes the error go away. But I have no idea if that correct :)

oozcitak commented 3 years ago

Any chance you can attach the image?

davidebbo commented 3 years ago

It's a kid picture so I'd rather not share publicly, but I can email it to you if you give me an address.

oozcitak commented 3 years ago

@davidebbo Thanks, I sent you an email.

oozcitak commented 3 years ago

Fixed and released a new version.