5,445,109 members and growing! (13,428 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

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

By David Boland

This article provides a simplified C# wrapper to the FreeImage project for graphical file format conversion.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Windows, .NET, Visual Studio, Dev

Posted: 30 Sep 2003
Updated: 30 Sep 2003
Views: 118,743
Bookmarked: 25 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 3.30 Rating: 3.90 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 57.1%
5

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



Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
Subject  Author Date 
Generalc# compact frmeworkmembervertex_x11:22 3 Jun '08  
QuestionHelp, I am getting zero file size as resultmemberRomashka_NN8:54 9 Apr '07  
AnswerRe: Help, I am getting zero file size as resultmemberNoEscom22:37 15 Nov '07  
GeneralResquest.OutputStreammemberkernellius13:03 4 Apr '07  
QuestionWhat about large images?memberGerrit Horeis4:17 6 Oct '06  
Questionhelpmemberxenia gr23:48 3 Sep '06  
QuestionHow to convert double pointers (**)?membervor0nwe6:37 26 Apr '06  
QuestionI can't make C# works with FreeImagemembermesh200522:41 5 Mar '06  
Generalinserting images in word document?memberHiral Patel0:51 27 Nov '05  
GeneralGrayscalememberEmailSolidale21:55 21 Sep '05  
GeneralC:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint'membereric paul13:12 2 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint'memberDavid Boland13:32 2 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint'memberminorello4:46 5 Aug '05  
GeneralRe: C:\projects\SVGTest\SVG.cs(145): Argument '2': cannot convert from 'int' to 'uint'memberDavid Boland6:53 5 Aug '05  
Generaland now FreeImaged.dll :(memberleoni5108:23 1 Jul '05  
GeneralRe: and now FreeImaged.dll :(memberDavid Boland8:35 1 Jul '05  
GeneralRe: and now FreeImaged.dll :(memberleoni51022:24 1 Jul '05  
GeneralRe: and now FreeImaged.dll :(memberalex_mp14:49 3 Dec '05  
GeneralI cant create FreeImage.dllmemberleoni5109:19 26 Jun '05  
GeneralRe: I cant create FreeImage.dllmemberianhfar7:01 30 Jun '05  
GeneralUnable to find an entry point (FreeImage_CloseMultiBitmap)memberjohn_b1231:58 21 Jun '05  
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap)memberDeeJayX1:53 13 Mar '06  
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap)memberDeeJayX1:53 13 Mar '06  
AnswerRe: Unable to find an entry point (FreeImage_CloseMultiBitmap)membervor0nwe6:30 26 Apr '06