Yesterday I got a request to open some jp2 files, so that they could be renamed depending on the exif information and then saved as jpeg.

Jpeg2000 was once supposed to replace jpeg because it was so much better than jpeg. Never happened and never will. But the format is out there and .Net has no native support for it.

These are the formats .Net supports.

Bmp Gets the bitmap (BMP) image format.
Emf Gets the enhanced metafile (EMF) image format.
Exif Gets the Exchangeable Image File (Exif) format.
Gif Gets the Graphics Interchange Format (GIF) image format.
Icon Gets the Windows icon image format.
Jpeg Gets the Joint Photographic Experts Group (JPEG) image format.
MemoryBmp Gets a memory bitmap image format.
Png Gets the W3C Portable Network Graphics (PNG) image format.
Tiff Gets the Tagged Image File Format (TIFF) image format.
Wmf Gets the Windows metafile (WMF) image format.

No jp2 as you can see. So I went to google for a solution. And I found one. It’s called FreeImage.

Which supports these formats.

Supported formats
BMP files [reading, writing]
Dr. Halo CUT files [reading] *
DDS files [reading]
EXR files [reading, writing]
Raw Fax G3 files [reading]
GIF files [reading, writing]
HDR files [reading, writing]
ICO files [reading, writing]
IFF files [reading]
JBIG [reading, writing] **
JNG files [reading]
JPEG/JIF files [reading, writing]
JPEG-2000 File Format [reading, writing]
JPEG-2000 codestream [reading, writing]
KOALA files [reading]
Kodak PhotoCD files [reading]
MNG files [reading]
PCX files [reading]
PBM/PGM/PPM files [reading, writing]
PFM files [reading, writing]
PNG files [reading, writing]
Macintosh PICT files [reading]
Photoshop PSD files [reading]
RAW camera files [reading]
Sun RAS files [reading]
SGI files [reading]
TARGA files [reading, writing]
TIFF files [reading, writing]
WBMP files [reading, writing]
XBM files [reading]
XPM files [reading, writing]
  • only grayscale

** only via external plugin, might require a commercial license

en Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today’s multimedia applications. FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).

It isn’t a managed .Net library but there is a wrapper for .Net.

So after downloading the whole thing I went to work.

You first set a reference to FreeImageNet.dll. And then you make sure that the FreeImage.dll appears in your application folder. Either by copy pasting it or by making it a part of your project and setting its “Copy to Output Directory” property to Copy Always and Build Action to none.

Now you just have to use the library.

```vbnet If Not FreeImage.IsAvailable() Then MessageBox.Show(“FreeImage.dll seems to be missing. Aborting.”) Else

        Dim o As New OpenFileDialog
        o.Filter = "jpeg2000|*.jp2"
        If o.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not dib.IsNull Then
            End If
            dib = FreeImage.LoadEx(o.FileName)
            Me.PictureBox1.Image = FreeImage.GetBitmap(dib)
            Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        End If
    End If```

The above code checks if the library works, opens a filedialog so you can select your file, opens the image in freeimage format, makes it into .Net Image and shows it in a picturebox in stretchmode.

And that’s it easy as pie.

But of course it didn’t work the first time around. Not because it doesn’t work but because I forgot to read the signs, big signs at that. My system is Win7 64x and the FreeImage dll is 32 bits so it kinda didn’t like that. Easily solved though. You just set the Active Solution Platform to x86 and you’re done.