Search code examples
c#.netimage-processingbitmapfreeimage

How to save a .NET Bitmap using FreeImage.NET


After spending 2 days to realize that the C# Bitmap.Save method was bugged (for JPEG/grayscale/8bbp), I tried FreeImage to see if I could save it correctly, and at first glance it seemed so, but after closer inspection it seems it doesn't work either.

Here are my tests:

If I do

FreeImage.SaveBitmap(aImage, aSavePath, FREE_IMAGE_FORMAT.FIF_JPEG, FREE_IMAGE_SAVE_FLAGS.DEFAULT);

the image DPI's aren't saved correctly and if I convert the Bitmap into a FIBITMAP (so that I can specify the DPI's

MemoryStream imageStream = new MemoryStream();
aImage.Save(imageStream, aImageFormat);

FIBITMAP dib = FreeImage.LoadFromStream(imageStream, FREE_IMAGE_LOAD_FLAGS.JPEG_ACCURATE, freeImageFormat);

FreeImage.SetResolutionX(dib, (uint)aImage.HorizontalResolution);
FreeImage.SetResolutionY(dib, (uint)aImage.VerticalResolution);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, aSavePath, FREE_IMAGE_SAVE_FLAGS.DEFAULT);

Any ideas on how I can save a Bitmap using FreeImage and preserve the DPI's and bpp's? Or is the FreeImage.Save method also bugged?


Solution

  • I gave up on this 2 options (FreeImage and Magick.NET) and went with GraphicsMagick.NET.