JPEG 2000 support in C#.NET

For anyone coming across this old post, the above code from Gordon works great, but as jixtra pointed out, you will indeed get an exception: System.DllNotFoundException: 'Unable to load DLL 'FreeImage': The specified module could not be found.' when installing via nuget. I was able to get it working in .net 4.6.1 by installing the FreeImage-dotnet-core nuget package and manually adding the FreeImage.dll to the bin folder. You can download the dll here: http://freeimage.sourceforge.net/download.html.

I needed a better quality image to use with tesseract so I made a few minor changes which made a huge difference to the quality of the new jpeg:

var jp2Format = FREE_IMAGE_FORMAT.FIF_JP2;
var dib = FreeImage.LoadEx("test.jp2", ref jp2Format);

FreeImage.SetResolutionX(dib, 300);
FreeImage.SetResolutionY(dib, 300);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);

// NOTE: memory needs to be explicitly freed (GC won't do this)
FreeImage.UnloadEx(ref dib);

Seems like we can do it using FreeImage (which is free)

FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk    
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);