0

I want to be able to write a huge amount of metadata to a jpeg but .NET is fighting me. I'm to the point where I wonder if it would be easier to just modify the bytes myself. There's no image.Metadata.Comment = "My comment";, I can't find any projects that do it for you (See this answer), Microsoft's documentation is confusing, another StackOverflow post led to this article which when you get to the end you find out it doesn't show you how to actually write metadata, and this code by John P works but if you try to add a lot of characters you get the error System.IO.FileFormatException: Commit unsuccessful because too much metadata changed..

So pretty much nothing is working at all. I want to add a comment, of any length, to my jpeg. So if the jpeg itself is 1.3MB I want to be able to add a comment so long that the jpeg becomes 10MB.

Community
  • 1
  • 1
  • 1
    Interesting, I was sure that standard would specify a limit to the size of EXIF fields, but couldn't find anything [here](http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf) – Avner Shahar-Kashtan Jan 16 '16 at 17:34
  • good find. [looks like](http://stackoverflow.com/questions/3248946/what-is-the-maximum-size-of-jpeg-metadata) you can store an arbitrary amount of data but you have to use some trickery. –  Jan 16 '16 at 17:43
  • You cannot store any amount of data in a JPEG without defining some kid of syntax for it. COM and APP markers have a 2-byte length field. – user3344003 Jan 16 '16 at 20:22

1 Answers1

1

You don't say what type of metadata you're trying to write. But from your question it sounds as though you're writing large strings into the JPEG comment section.

JPEG files are basically a list of segments. These segments have a type identifier (a single byte) and a length (two bytes). This means the longest segment can only be 65535 bytes in length.

You can store comments in their own segment, the so-called COM segment.

If your comment is longer than 65535 bytes, then you can store multiple COM segments in the file. The reader is supposed to concatenate these together into the final comment.

Some discussion here.

As for how to do this in C#, I'm not aware of any library that supports this. I wrote and maintain MetadataExtractor for .NET and Java, but as the name suggests it's all about reading, not writing, metadata.

However the container format for JPEG is not too complicated. It shouldn't be too complicated to write your own code that injects COM segments into the file and copies all other segments in verbatim.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742