Click here to Skip to main content
11,584,612 members (72,222 online)
Click here to Skip to main content

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

, 30 Sep 2003 220.8K 3.5K 41
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

You may also be interested in...

Comments and Discussions

 
GeneralRe: Displaying in .NET Pin
schoeneck10-Nov-03 11:22
memberschoeneck10-Nov-03 11:22 
GeneralRe: Displaying in .NET Pin
David Boland10-Nov-03 22:58
memberDavid Boland10-Nov-03 22:58 
GeneralRe: Displaying in .NET Pin
Drolon17-Nov-03 7:15
memberDrolon17-Nov-03 7:15 
GeneralRe: Displaying in .NET Pin
furkinfedup23-Jan-04 23:26
memberfurkinfedup23-Jan-04 23:26 
GeneralRe: Displaying in .NET Pin
persée23-Aug-05 4:41
memberpersée23-Aug-05 4:41 
GeneralMemory or File... Pin
Drew Stainton1-Oct-03 7:46
memberDrew Stainton1-Oct-03 7:46 
GeneralRe: Memory or File... Pin
Anonymous1-Oct-03 22:38
sussAnonymous1-Oct-03 22:38 
QuestionHow to draw FIBITMAP to form. Pin
Skyer1-Oct-03 4:30
memberSkyer1-Oct-03 4:30 
Hi, guys

How to draw FIBITMAP to a form.
If I load with FreeImage function "Load", how to draw it up?

How to change FIBITAMP to .NET class "Bitmap"?



With best regards,
Skyer
AnswerRe: How to draw FIBITMAP to form. Pin
David Boland1-Oct-03 5:09
memberDavid Boland1-Oct-03 5:09 
GeneralAccessing the image Pin
Ray Hayes1-Oct-03 1:36
memberRay Hayes1-Oct-03 1:36 
GeneralRe: Accessing the image Pin
David Boland1-Oct-03 5:20
memberDavid Boland1-Oct-03 5:20 
GeneralSystem::Drawing::Image Pin
Jonathan de Halleux1-Oct-03 1:36
memberJonathan de Halleux1-Oct-03 1:36 
GeneralRe: System::Drawing::Image Pin
David Boland1-Oct-03 5:15
memberDavid Boland1-Oct-03 5:15 

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
Web03 | 2.8.150707.1 | Last Updated 1 Oct 2003
Article Copyright 2003 by David Boland
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid