Reputation:
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.
Upvotes: 0
Views: 676
Reputation: 310832
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.
Upvotes: 1