aKzenT
aKzenT

Reputation: 7895

How do I add JPEG comment (COM) to an image?

I'm trying to add a JPEG comment to an image file using WPF. Trying the following code throws me an ArgumentOutOfRangeException. Setting other properties works without problems.

    using (Stream read = File.OpenRead(@"my.jpeg"))
    {
        JpegBitmapDecoder decoder = new JpegBitmapDecoder(read, BitmapCreateOptions.None, BitmapCacheOption.None);

        var meta = decoder.Frames[0].Metadata.Clone() as BitmapMetadata;
        meta.SetQuery("/app1/ifd/exif:{uint=40092}", "xxx"); // works
        meta.SetQuery("/com/TextEntry", "xxx"); // does not work
    }

To be clear: I have to set the /com/TextEntry field which is listed in MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ee719904%28v=vs.85%29.aspx#_jpeg_metadata

The data is read by another application which only supports this tag, so it is not an option to use other "comment" fields.

Any ideas?

Upvotes: 4

Views: 1227

Answers (1)

Hans Passant
Hans Passant

Reputation: 942000

The data type for /com/TextEntry is a bit tricky, it requires an LPSTR. Which is a raw 8-bit encoded string pointer. You can do this by passing a char[] for the argument. Fix:

   meta.SetQuery("/com/TextEntry", "xxx".ToCharArray());

Do note that text encoding might be an issue if you use non-ASCII characters, you'll get text encoded in the machine's default code page (Encoding.Default).

Upvotes: 5

Related Questions