5

I am trying to convert a TIFF file to JPEG using FreeImage.

The problem is that FreeIamge.SaveToStream doesn't actually do anything. Even after the call, stream has a Length, Capacity and Position of 0.

This is my code:

using (var stream = new MemoryStream())
{
    var image = FreeImage.LoadEx(fileName);
    FreeImage.SaveToStream(image, stream, FREE_IMAGE_FORMAT.FIF_JPEG,
                           FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);

    // stream.Length, stream.Capacity & stream.Position are all 0 here
}

What am I doing wrong?

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443

1 Answers1

1

The problem was the input - a 16 bit image created by another image library. It looks like FreeImage has some problems with 16 bit images as GDI+ could read it without problems. I switched the input to a 24 bit image and the code in my question started working.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Sigh... the joys of libraries that are not using exception handling. [Seems that `SaveToStream` returns a `bool` for success/failure](http://freeimage.sourceforge.net/fnet/html/4A01CFCD.htm). – Uwe Keim Sep 29 '14 at 08:16
  • "GDI+ could read it without problems" -- GDI+ could read a 16-bit TIFF? My experience with PNG files (I just ran into this) is that System.Drawing.Image (.NET wrapper for GDI+) downconverts 16-bit to 8-bit and multiplies single-channel to RGBA, but I haven't tried TIFF yet. – Mike C Apr 17 '15 at 01:23