Click here to Skip to main content
15,880,469 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 287.3K   4.6K   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

 
QuestionTo those getting errors. Pin
Dave Simon17-Jun-21 10:12
Dave Simon17-Jun-21 10:12 
QuestionConversion of JPG to TGA gives zero KB TGA file Pin
dineshlepakshi16-Dec-13 14:24
dineshlepakshi16-Dec-13 14:24 
QuestionHow can I convert to JPG 2000 Pin
AmerSawan28-Dec-12 4:21
AmerSawan28-Dec-12 4:21 
GeneralStill no luck on Windows Mobile 6 Pin
Paul Kamath29-Oct-10 6:13
Paul Kamath29-Oct-10 6:13 
Any feedback is greatly appreciated.
Generalfreeimage.dll must be in your windows\system32 folder Pin
Paul Kamath29-Oct-10 2:47
Paul Kamath29-Oct-10 2:47 
Generalc# compact frmework Pin
mightyCoCo3-Jun-08 10:22
mightyCoCo3-Jun-08 10:22 
GeneralRe: c# compact frmework Pin
Riyas Aboobaker24-Mar-09 6:28
Riyas Aboobaker24-Mar-09 6:28 
GeneralRe: c# compact frmework Pin
Riyas Aboobaker24-Mar-09 18:39
Riyas Aboobaker24-Mar-09 18:39 
QuestionHelp, I am getting zero file size as result Pin
Romashka_NN9-Apr-07 7:54
Romashka_NN9-Apr-07 7:54 
AnswerRe: Help, I am getting zero file size as result Pin
NoEscom15-Nov-07 21:37
NoEscom15-Nov-07 21:37 
GeneralResquest.OutputStream Pin
kernellius4-Apr-07 12:03
kernellius4-Apr-07 12:03 
QuestionWhat about large images? Pin
Gerrit Horeis6-Oct-06 3:17
Gerrit Horeis6-Oct-06 3:17 
Questionhelp Pin
xenia gr3-Sep-06 22:48
xenia gr3-Sep-06 22:48 
QuestionHow to convert double pointers (**)? Pin
vor0nwe26-Apr-06 5:37
vor0nwe26-Apr-06 5:37 
QuestionI can't make C# works with FreeImage Pin
mesh20055-Mar-06 21:41
mesh20055-Mar-06 21:41 
Questioninserting images in word document? Pin
Hiral Patel26-Nov-05 23:51
Hiral Patel26-Nov-05 23:51 
GeneralGrayscale Pin
EmailSolidale21-Sep-05 20:55
EmailSolidale21-Sep-05 20:55 
GeneralC:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
eric paul2-Aug-05 12:12
eric paul2-Aug-05 12:12 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
David Boland2-Aug-05 12:32
David Boland2-Aug-05 12:32 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
minorello5-Aug-05 3:46
minorello5-Aug-05 3:46 
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 

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.