2

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?

Gonçalo Cardoso
  • 2,253
  • 4
  • 34
  • 63
  • If you want to "preserve details" then you don't want to use JPEG. And you most certainly don't want to compress it twice. Setting the resolution for a bitmap is a pretty meaningless thing to do. It is just a reference number that a program that reads the image can use to display it in its original physical size. That helps when you have a high-resolution "retina" display, it avoids the image being displayed as a postage stamp. It does absolutely nothing to the quality of the image and is meaningless when you convert images. – Hans Passant Jul 24 '14 at 10:00
  • Edited the question to clarify the details that I need to keep – Gonçalo Cardoso Jul 24 '14 at 10:21
  • "Preserve bpp" is meaningless as well, JPEG only supports 24bpp. – Hans Passant Jul 24 '14 at 10:33
  • 1
    No, it also supports 8 and 32 bpp – Gonçalo Cardoso Jul 24 '14 at 11:05
  • No, a later standard called JPEG-2000 (aka JP2) is required to get support for additional pixel formats. It is widely ignored, few programs can read it. – Hans Passant Jul 24 '14 at 11:11
  • As Photoshop allowed me to save in 8 bits I assumed it was standard. Regarding the question, do you have any idea on how I can keep the bpp and DPI? Because I have a client that demanded they receive the images in jpeg/8bbp/grayscale – Gonçalo Cardoso Jul 24 '14 at 12:25
  • Clearly you are working from a pretty lousy functional specification. I have no reasonable guess at what that might mean. You should of course ask the client for clarification. – Hans Passant Jul 24 '14 at 12:53

2 Answers2

0

Some software relies on metadata while getting resolution, so you can try to add EXIF with DPI fields to jpeg created with FreeImage.

P.S. Your task seems to be pretty easy, I’m sure you can find a few more SDKs for that.

Eugene
  • 3,335
  • 3
  • 36
  • 44
  • From all the SDK's that I tested (must be free), FreeImage was the only one that could save to 8bpp jpeg – Gonçalo Cardoso Jul 25 '14 at 07:08
  • Did you try Magick.NET, a .NET wrapper around ImageMagick? I know IM can save gray jpegs. – Eugene Jul 25 '14 at 07:30
  • Tried MagickImage and it seems to work, but there's a BIG problem, when I do MagickImage image1 = new MagickImage(aPathToImage) do load my image (also tried loading a Bitmap) it takes 5 seconds to load it... is this normal? – Gonçalo Cardoso Jul 25 '14 at 09:07
  • Just made a few more tests and realized that it always takes 5 seconds to execute ANY FIRST call to MagickImage. Is this the normal behavior? – Gonçalo Cardoso Jul 25 '14 at 09:24
  • It sounds like the OpenCL benchmarking is not saved to disk. You are probably better off disabling it. If you need more help start a topic here: https://magick.codeplex.com/discussions. – dlemstra Jul 27 '14 at 21:18
0

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

Gonçalo Cardoso
  • 2,253
  • 4
  • 34
  • 63