Click here to Skip to main content
Licence 
First Posted 30 Sep 2003
Views 183,425
Downloads 1,840
Bookmarked 38 times

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

By David Boland | 30 Sep 2003
This article provides a simplified C# wrapper to the FreeImage project for graphical file format conversion.
2 votes, 25.0%
1
1 vote, 12.5%
2

3

4
5 votes, 62.5%
5
4.00/5 - 8 votes
μ 4.00, σa 3.36 [?]

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

About the Author

David Boland

Web Developer

United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralStill no luck on Windows Mobile 6 PinmemberPaul Kamath7:13 29 Oct '10  
Generalfreeimage.dll must be in your windows\system32 folder PinmemberPaul Kamath3:47 29 Oct '10  
Generalc# compact frmework Pinmembervertex_x11:22 3 Jun '08  
GeneralRe: c# compact frmework PinmemberRiyas Aboobaker7:28 24 Mar '09  
GeneralRe: c# compact frmework PinmemberRiyas Aboobaker19:39 24 Mar '09  
QuestionHelp, I am getting zero file size as result PinmemberRomashka_NN8:54 9 Apr '07  
AnswerRe: Help, I am getting zero file size as result PinmemberNoEscom22:37 15 Nov '07  
GeneralResquest.OutputStream Pinmemberkernellius13:03 4 Apr '07  
QuestionWhat about large images? PinmemberGerrit Horeis4:17 6 Oct '06  
Questionhelp Pinmemberxenia gr23:48 3 Sep '06  
QuestionHow to convert double pointers (**)? Pinmembervor0nwe6:37 26 Apr '06  
QuestionI can't make C# works with FreeImage Pinmembermesh200522:41 5 Mar '06  
Questioninserting images in word document? PinmemberHiral Patel0:51 27 Nov '05  
GeneralGrayscale PinmemberEmailSolidale21:55 21 Sep '05  
GeneralC:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pinmembereric paul13:12 2 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' PinmemberDavid Boland13:32 2 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' Pinmemberminorello4:46 5 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint' PinmemberDavid Boland6:53 5 Aug '05  
Generaland now FreeImaged.dll :( Pinmemberleoni5108:23 1 Jul '05  
GeneralRe: and now FreeImaged.dll :( PinmemberDavid Boland8:35 1 Jul '05  
GeneralRe: and now FreeImaged.dll :( Pinmemberleoni51022:24 1 Jul '05  
GeneralRe: and now FreeImaged.dll :( Pinmemberalex_mp14:49 3 Dec '05  
GeneralI cant create FreeImage.dll Pinmemberleoni5109:19 26 Jun '05  
GeneralRe: I cant create FreeImage.dll Pinmemberianhfar7:01 30 Jun '05  
GeneralUnable to find an entry point (FreeImage_CloseMultiBitmap) Pinmemberjohn_b1231:58 21 Jun '05  

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.

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