0

I'm handling a lot of different image formats within my application. Luckily I was able to source out the biggest part of dealing with image formats to C#WPF. However, I've got an issue when saving images:

I'd like to save my images as JPEGs using JpegBitmapEncoder with an RGB profile. This works all fine for color image formats. However, when handling images i.e. of format Gray16 or Gray8, the resulting JPEGs will have a Grayscale profile. I really do need an RGB profile for my JPEGs!

I know that I could just create a new WriteableBitmap in Bgra32, copy to it the data and use it to create the JPEG. This however means, that I would need to handle the different image formats myself. Most importantly I believe that this detour would be quite inefficient computationally (I need to convert a lot of image data!).

Also I can't use any solutions outside of C#/WPF (like Imagemagick).

I hope that there's a way to solve this easily and efficiently. I found no way to configure JpegBitmapEncoder for this and I had a try with ColorConvertedBitmap but to no avail!

Any ideas?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Do you want to color the greyscale image or just to save it with RGB channels but still with grey colors? – RononDex Jan 15 '14 at 11:25
  • Did you also try out [FormatConvertedBitmap](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.formatconvertedbitmap.aspx)? – Clemens Jan 15 '14 at 11:29
  • I need to save it with RGB channels but still with grey colors, will have a look at FormatConvertedBitmap – beta vulgaris Jan 15 '14 at 11:42

1 Answers1

0

The hint for FormatConvertedBitmap gave me the solution to my problem! Thanks RononDex!

FormatConvertedBitmap convertImg = new FormatConvertedBitmap(img, PixelFormats.Bgra32, null, 0);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(convertImg));
encoder.Save(stream);