Click here to Skip to main content
11,429,687 members (71,369 online)
Click here to Skip to main content

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

, 30 Sep 2003
Rate this:
Please Sign up or sign in to vote.
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:

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:

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

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

Share

About the Author

David Boland
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionConversion of JPG to TGA gives zero KB TGA file Pin
dineshlepakshi16-Dec-13 15:24
memberdineshlepakshi16-Dec-13 15:24 
QuestionHow can I convert to JPG 2000 Pin
AmerSawan28-Dec-12 5:21
memberAmerSawan28-Dec-12 5:21 
GeneralStill no luck on Windows Mobile 6 Pin
Paul Kamath29-Oct-10 7:13
memberPaul Kamath29-Oct-10 7:13 
Generalfreeimage.dll must be in your windows\system32 folder Pin
Paul Kamath29-Oct-10 3:47
memberPaul Kamath29-Oct-10 3:47 
Generalc# compact frmework Pin
vertex_x3-Jun-08 11:22
membervertex_x3-Jun-08 11:22 
GeneralRe: c# compact frmework Pin
Riyas Aboobaker24-Mar-09 7:28
memberRiyas Aboobaker24-Mar-09 7:28 
GeneralRe: c# compact frmework Pin
Riyas Aboobaker24-Mar-09 19:39
memberRiyas Aboobaker24-Mar-09 19:39 
QuestionHelp, I am getting zero file size as result Pin
Romashka_NN9-Apr-07 8:54
memberRomashka_NN9-Apr-07 8:54 
AnswerRe: Help, I am getting zero file size as result Pin
NoEscom15-Nov-07 22:37
memberNoEscom15-Nov-07 22:37 
GeneralResquest.OutputStream Pin
kernellius4-Apr-07 13:03
memberkernellius4-Apr-07 13:03 
QuestionWhat about large images? Pin
Gerrit Horeis6-Oct-06 4:17
memberGerrit Horeis6-Oct-06 4:17 
Questionhelp Pin
xenia gr3-Sep-06 23:48
memberxenia gr3-Sep-06 23:48 
QuestionHow to convert double pointers (**)? Pin
vor0nwe26-Apr-06 6:37
membervor0nwe26-Apr-06 6:37 
QuestionI can't make C# works with FreeImage Pin
mesh20055-Mar-06 22:41
membermesh20055-Mar-06 22:41 
Questioninserting images in word document? Pin
Hiral Patel27-Nov-05 0:51
memberHiral Patel27-Nov-05 0:51 
GeneralGrayscale Pin
EmailSolidale21-Sep-05 21:55
memberEmailSolidale21-Sep-05 21:55 
GeneralC:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
eric paul2-Aug-05 13:12
membereric paul2-Aug-05 13:12 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
David Boland2-Aug-05 13:32
memberDavid Boland2-Aug-05 13:32 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
minorello5-Aug-05 4:46
memberminorello5-Aug-05 4:46 
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pin
David Boland5-Aug-05 6:53
memberDavid Boland5-Aug-05 6:53 
Generaland now FreeImaged.dll :( Pin
leoni5101-Jul-05 8:23
memberleoni5101-Jul-05 8:23 
GeneralRe: and now FreeImaged.dll :( Pin
David Boland1-Jul-05 8:35
memberDavid Boland1-Jul-05 8:35 
GeneralRe: and now FreeImaged.dll :( Pin
leoni5101-Jul-05 22:24
memberleoni5101-Jul-05 22:24 
GeneralRe: and now FreeImaged.dll :( Pin
alex_mp3-Dec-05 14:49
memberalex_mp3-Dec-05 14:49 
GeneralI cant create FreeImage.dll Pin
leoni51026-Jun-05 9:19
memberleoni51026-Jun-05 9:19 
GeneralRe: I cant create FreeImage.dll Pin
ianhfar30-Jun-05 7:01
memberianhfar30-Jun-05 7:01 
GeneralUnable to find an entry point (FreeImage_CloseMultiBitmap) Pin
john_b12321-Jun-05 1:58
memberjohn_b12321-Jun-05 1:58 
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
DeeJayX13-Mar-06 1:53
memberDeeJayX13-Mar-06 1:53 
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
DeeJayX13-Mar-06 1:53
memberDeeJayX13-Mar-06 1:53 
AnswerRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) Pin
vor0nwe26-Apr-06 6:30
membervor0nwe26-Apr-06 6:30 
GeneralDDS and Freeimage Pin
cococvg27-Apr-05 1:23
membercococvg27-Apr-05 1:23 
GeneralUnable to load DLL (FreeImage.dll). Pin
TechTiger24-Mar-04 15:00
memberTechTiger24-Mar-04 15:00 
GeneralRe: Unable to load DLL (FreeImage.dll). Pin
David Boland25-Mar-04 0:55
memberDavid Boland25-Mar-04 0:55 
GeneralDisplaying in .NET Pin
furkinfedup26-Oct-03 2:37
memberfurkinfedup26-Oct-03 2:37 
GeneralRe: Displaying in .NET Pin
schoeneck10-Nov-03 12:22
memberschoeneck10-Nov-03 12:22 
GeneralRe: Displaying in .NET Pin
David Boland10-Nov-03 23:58
memberDavid Boland10-Nov-03 23:58 
GeneralRe: Displaying in .NET Pin
Drolon17-Nov-03 8:15
memberDrolon17-Nov-03 8:15 
GeneralRe: Displaying in .NET Pin
furkinfedup24-Jan-04 0:26
memberfurkinfedup24-Jan-04 0:26 
GeneralRe: Displaying in .NET Pin
persée23-Aug-05 5:41
memberpersée23-Aug-05 5:41 
GeneralMemory or File... Pin
Drew Stainton1-Oct-03 8:46
memberDrew Stainton1-Oct-03 8:46 
GeneralRe: Memory or File... Pin
Anonymous1-Oct-03 23:38
sussAnonymous1-Oct-03 23:38 
QuestionHow to draw FIBITMAP to form. Pin
Skyer1-Oct-03 5:30
memberSkyer1-Oct-03 5:30 
AnswerRe: How to draw FIBITMAP to form. Pin
David Boland1-Oct-03 6:09
memberDavid Boland1-Oct-03 6:09 
GeneralAccessing the image Pin
Ray Hayes1-Oct-03 2:36
memberRay Hayes1-Oct-03 2:36 
GeneralRe: Accessing the image Pin
David Boland1-Oct-03 6:20
memberDavid Boland1-Oct-03 6:20 
GeneralSystem::Drawing::Image Pin
Jonathan de Halleux1-Oct-03 2:36
memberJonathan de Halleux1-Oct-03 2:36 
GeneralRe: System::Drawing::Image Pin
David Boland1-Oct-03 6:15
memberDavid Boland1-Oct-03 6:15 
This wrapper is simply an API to convert between images formats. My own particular problem was that I had an image format that was not suitable for programatically importing into a Word document using the Selection.InlineShapes.AddPicture method. Therefore I used this API to convert the image into a standard Bitmap. Once converted I could then potentially use the Image or Bitmap classes to access them from C#, however this I feel is out of the scope of this Article.

David

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 1 Oct 2003
Article Copyright 2003 by David Boland
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid