Click here to Skip to main content
6,594,932 members and growing! (15,723 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, Visual Studio, Dev
Posted:30 Sep 2003
Views:141,131
Bookmarked:35 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.30 Rating: 3.90 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2

3

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


Member

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.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
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  
Generalinserting 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  
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) PinmemberDeeJayX1:53 13 Mar '06  
GeneralRe: Unable to find an entry point (FreeImage_CloseMultiBitmap) PinmemberDeeJayX1:53 13 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2003
Editor: Smitha Vijayan
Copyright 2003 by David Boland
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project