JPEG 2000 support in C#.NET
Asked Answered
L

6

25

It seems that .NET can't open JP2 (Jpeg 2000) files using the GDI library. I've searched on google but can't find any libraries or example code to do this.

Anybody got any ideas? I don't really want to pay for a library to do it unless I have to..

Lynching answered 26/2, 2009 at 13:11 Comment(0)
L
22

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);
Lynching answered 21/7, 2010 at 18:10 Comment(2)
thanks for guiding me. +1 for ur answer. But after rendering jpeg200 images on my window form, I am getting exception due to dib as null.**An exception was thrown in the rendering pipeline. ---> System.ArgumentNullException: Value cannot be null. Parameter name: dib at FreeImageAPI.FreeImage.GetBitmap(FIBITMAP dib, Boolean copyMetadata) at FreeImageAPI.FreeImage.GetBitmap(FIBITMAP dib)** . While debugging,found out that method static FIBITMAP LoadEx(string filename, FREE_IMAGE_LOAD_FLAGS flags, ref FREE_IMAGE_FORMAT format) returns me a structure {0} which causes the above exception.Applewhite
I get a module not found exception with the latest FreeImage nuget packageWedekind
B
3

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.

Berchtesgaden answered 26/2, 2009 at 13:20 Comment(0)
K
3

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);
Knighthood answered 14/1, 2018 at 22:46 Comment(0)
E
2

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.

Edbert answered 25/11, 2009 at 1:33 Comment(0)
P
2

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);
}
Problem answered 25/1, 2019 at 3:24 Comment(0)
K
1

Also, for a currently-up-to-date, open-source option you can use Emgu CV, which is a wrapper around OpenCV. Basic example code in C# looks like:

Mat image = CvInvoke.Imread(@"\Path\To\File.jp2");
Kalli answered 3/3, 2022 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.