Click here to Skip to main content
16,012,153 members
Articles / Programming Languages / C#
Article

C# Wrapper to the FreeImage DLL for graphical image format conversion

Rate me:
Please Sign up or sign in to vote.
4.67/5 (10 votes)
30 Sep 2003 292.1K   4.7K   42   49
This article provides a simplified C# wrapper to the FreeImage project for graphical file format conversion.

Introduction

FreeImage is an extremely useful Open Source project for reading, manipulating and converting a large amount of graphical formats. However at present the library exists as Win32 Dynamic Link Library. As my latest fascination is C#, I decided to create a simple Interop wrapper for it.

Using the code

The code is very straightforward to use, which is a reflection on the FreeImage implementation itself. For my needs I wanted to be able to take a picture file in Portable Bitmap (PBM) format and convert it to a standard Bitmap (BMP). For this I needed 3 methods exposed via the FreeImage DLL, FreeImage_Load, FreeImage_Save and FreeImage_Unload, the signature for which are:

C#
DLL_API FIBITMAP *DLL_CALLCONV FreeImage_Load(FREE_IMAGE_FORMAT fif, 
       const char *filename, int flags FI_DEFAULT(0));
 
DLL_API BOOL DLL_CALLCONV FreeImage_Save(FREE_IMAGE_FORMAT fif, 
   FIBITMAP *dib, const char *filename, int flags FI_DEFAULT(0)); 
 
DLL_API void DLL_CALLCONV FreeImage_Unload(FIBITMAP* dib);

Now the C# Interop signatures for these are:

C#
public class FreeImage
{
     [DllImport("FreeImage.dll")]
     public static extern int FreeImage_Load(FIF format,
                    string filename, int flags);

     [DllImport("FreeImage.dll")]
     public static extern void FreeImage_Unload(int handle);

     [DllImport("FreeImage.dll")]
     public static extern bool FreeImage_Save(FIF format,
        int handle, string filename, int flags);
}

As an example, to convert a graphical image from PBM to BMP, all you need to do is the following (note the enum FIF is not documented here, but can be found in the source code).

C#
namespace Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int handle = FreeImageAPI.FreeImage.FreeImage_Load(
                FreeImageAPI.FIF.FIF_PBM,
                @"c:\image.pbm", 
                0);
                          
            FreeImageAPI.FreeImage.FreeImage_Save(
                FreeImageAPI.FIF.FIF_BMP,   
                handle, 
                @"c:\image.bmp", 
                0);
 
            FreeImageAPI.FreeImage.FreeImage_Unload(handle);          
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
David Boland5-Aug-05 5:53
David Boland5-Aug-05 5:53 
Generaland now FreeImaged.dll :( Pin
leoni5101-Jul-05 7:23
leoni5101-Jul-05 7:23 
GeneralRe: and now FreeImaged.dll :( Pin
David Boland1-Jul-05 7:35
David Boland1-Jul-05 7:35 
GeneralRe: and now FreeImaged.dll :( Pin
leoni5101-Jul-05 21:24
leoni5101-Jul-05 21:24 
GeneralRe: and now FreeImaged.dll :( Pin
alex_mp3-Dec-05 13:49
alex_mp3-Dec-05 13:49 
GeneralI cant create FreeImage.dll Pin
leoni51026-Jun-05 8:19
leoni51026-Jun-05 8:19 
GeneralRe: I cant create FreeImage.dll Pin
ianhfar30-Jun-05 6:01
ianhfar30-Jun-05 6:01 
GeneralRe: I cant create FreeImage.dll Pin
Dave Simon17-Jun-21 10:12
Dave Simon17-Jun-21 10:12 
Hi, I too saw that the FreeImage.dll is missing.

In these links you will find much better samples and all the files necessary to successfully build and run the FreeImage samples. They are from the actual creator of FreeImage.

The official site is The FreeImage Project[^]

You want the second link and perhaps the third, which is the official documentation.
The important 2nd link is titled Binary distribution includes DLL, linkage LIB, header file, C++, C#, Delphi and VB6 wrappers, and examples.

Download that zip and make sure you right click it, select properties then check the box that is titled Unblock

In that zip is the missing dlls, samples and wrappers for .NET.

It's also important to note you may need to add the FreeImage.dll to your project then right click on FreeImage.dll and select it build properties as Content then Copy If Newer. That will add the FreeImage.dll to your proper place in your project and Visual Studio will indicate to your project that FreeImage.dll is part of the project.

If you have build issues make sure that your project build properties match that of the FreeImage.dll. Example using x64 FreeImage.dll make sure your project build properties are set to x64 as well.

Some also indicated that FreeImage.dll needs to be in Windows\System folder but I didn't find that to be the case. The easiest way is download the official samples (link provided above) from the actual author of freeimage and those should run fine.
GeneralUnable to find an entry point (FreeImage_CloseMultiBitmap) Pin
john_b12321-Jun-05 0:58
john_b12321-Jun-05 0:58 
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
asd00713-Mar-06 0:53
asd00713-Mar-06 0:53 
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
asd00713-Mar-06 0:53
asd00713-Mar-06 0:53 
AnswerRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
vor0nwe26-Apr-06 5:30
vor0nwe26-Apr-06 5:30 
GeneralDDS and Freeimage Pin
cococvg27-Apr-05 0:23
cococvg27-Apr-05 0:23 
GeneralUnable to load DLL (FreeImage.dll). Pin
TechTiger24-Mar-04 14:00
TechTiger24-Mar-04 14:00 
GeneralRe: Unable to load DLL (FreeImage.dll). Pin
David Boland24-Mar-04 23:55
David Boland24-Mar-04 23:55 
GeneralDisplaying in .NET Pin
furkinfedup26-Oct-03 1:37
furkinfedup26-Oct-03 1:37 
GeneralRe: Displaying in .NET Pin
Andreas Schoeneck10-Nov-03 11:22
Andreas Schoeneck10-Nov-03 11:22 
GeneralRe: Displaying in .NET Pin
David Boland10-Nov-03 22:58
David Boland10-Nov-03 22:58 
GeneralRe: Displaying in .NET Pin
Drolon17-Nov-03 7:15
Drolon17-Nov-03 7:15 
GeneralRe: Displaying in .NET Pin
furkinfedup23-Jan-04 23:26
furkinfedup23-Jan-04 23:26 
GeneralRe: Displaying in .NET Pin
persée23-Aug-05 4:41
persée23-Aug-05 4:41 
GeneralMemory or File... Pin
Drew Stainton1-Oct-03 7:46
Drew Stainton1-Oct-03 7:46 
GeneralRe: Memory or File... Pin
Anonymous1-Oct-03 22:38
Anonymous1-Oct-03 22:38 
QuestionHow to draw FIBITMAP to form. Pin
Skyer1-Oct-03 4:30
Skyer1-Oct-03 4:30 
AnswerRe: How to draw FIBITMAP to form. Pin
David Boland1-Oct-03 5:09
David Boland1-Oct-03 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.