4

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?

aKzenT
  • 7,775
  • 2
  • 36
  • 65
  • 1
    I don't think the JPEG Comments reader/writer is supported by WPF. From the [SetQuery](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapmetadata.setquery(v=vs.110).aspx) method docs: `Windows Presentation Foundation (WPF) supports the following image metadata schemas: Exchangeable image file (Exif), tEXt (PNG Textual Data), image file directory (IFD), International Press Telecommunications Council (IPTC), and Extensible Metadata Platform (XMP).` – DrDeth Jan 17 '14 at 15:37

1 Answers1

5

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).

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • How about `System.Text.Encoding.ASCII.GetBytes("xxx")`? That would return a `byte[]`. – Clemens Jan 17 '14 at 16:13
  • No, that doesn't generate an LPSTR. – Hans Passant Jan 17 '14 at 16:18
  • Genius! That does the trick! I searched a long time finding any references on how to set this tag and even google did not have any reference to /com/TextEntry apart from the MSDN page above. Interestingly enough, when using GetQuery("/com/TextEntry") it will return the data as a string and not a char[]... – aKzenT Jan 17 '14 at 16:29