JPEG 2000 support in C#.NET

*爱你&永不变心* 提交于 2019-11-27 04:31:07

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);
Rowland Shaw

I was looking for something similar a while back, with a view to implementing one if I could; The responses to my question imply that there is no documented method to do this for GDI+ which the Image class in .Net uses.

I believe that if you're writing a WPF application, then you can extend the list of supported image formats through Windows Imanging Components codecs, and there may be one out there already (ask your local friendly search engine?)

There is an option to use an addon such as DotImage which supports JPEG2000, although there may be more "effort" involved in loading images.

I've used Leadtools to display JPEG 2000 images. They provide a .NET library including WPF and WinForms controls to display the images. However, there is a reasonably steep price tag.

Joachim Kerschbaumer

Maybe you should checkout this project.

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);

You can use Jpeg2000.Net library if you need a fully managed solution without unsafe blocks. Disclaimer: I am working on this library, the library is commercial.

Here is the basic sample for decoding of JPEG 2000 image to TIFF:

string fileName = ...; // path to JPEG 2000 image
using (var image = new J2kImage(fileName))
{
    var options = new J2kDecodingOptions
    {
        UpsampleComponents = true
    };

    // Alternatively, you can decode only part of the image using J2kImage.DecodeArea method
    var imageData = image.Decode(options);
    imageData.Save(tiffFileName, J2kOutputFormat.Tiff);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!