I need to add a metadata tag (description) to the uploaded images.
I found this answer: https://stackoverflow.com/a/318216/2/ which is great for JPG files, but not PNG.
private string Tag = "test meta data"; private static Stream TagImage(Stream input, string type) { bool isJpg = type.EndsWith("jpg", StringComparison.InvariantCultureIgnoreCase) || type.EndsWith("jpeg", StringComparison.InvariantCultureIgnoreCase); bool isPng = type.EndsWith("png", StringComparison.InvariantCultureIgnoreCase); BitmapDecoder decoder = null; if (isJpg) { decoder = new JpegBitmapDecoder(input, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } else if (isPng) { decoder = new PngBitmapDecoder(input, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); } else { return input; }
It works fine when I load jpg, but with png in the metaData.Subject = Tag it throws a System.NotSupportedException (this codec does not support the specified property).
Update
It seems I need to use another method based on the image format:
if (isJpg) { metaData.SetQuery("/app1/ifd/exif:{uint=270}", Tag); } else { metaData.SetQuery("/tEXt/{str=Description}", Tag); }
Based on requests for available formats, the former should work for both formats. The second one does not work either (it creates metadata in the image, but does not save its value).
If I try to use the first method ( /app1/ifd/exif ) for PNG, in the encoder.Save line I get an exception that is not supported, "not suitable for image processing component".
source share