Click here to Skip to main content
12,823,618 members (48,897 online)
Click here to Skip to main content
Add your own
alternative version


64 bookmarked
Posted 7 Jul 2002

Image Format Conversion in .NET

, 7 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Discusses how to convert image formats in .NET, exemplified by a WebService that converts image formats on the fly!


.NET is productive! There's no doubt in that. The framework contains huge number of classes, which encapsulate many of the tasks required by the developer during the course of development. Take for instance, the TCPListener class that has simplified design of a TCP server to few lines of code, from the huge bunch that was written back in the days of Winsock. Another good example is that of working with images, in particular, converting images from one format to another, which is the main focus of this article.

I shall start off by introducing to the classes involved in working with images and their conversion, and show you how that is done. Finally, we build a real-life application: a .NET XML Webservice that shall convert images from one file format to another, on the fly! Next, we shall also have a look at a very simple C# client that shall invoke the webservice and convert images using it. So, without any further delay, lets get started.

Creating and converting image

The very first step in working with images is to create an object of the Image class object, which is present in the System.Drawing namespace. Since converting image file format requires the image to be present as a file, we shall create the Image object using the FromFile static method of the class, as shown below:

Image imgInFile=Image.FromFile(strFileName);

strFileName is the complete path to the image file. Now that we have an Image object ready, the second line of code that converts the image to a specified file format is this:


The first parameter to the Save method is the complete path to the output filename, while the second parameter is an enumeration, defined in System.Drawing.Imaging, that lets you specify the format in which the file should be saved. The above code saves the image as a .BMP file, but there are 8 more image file formats in which the image can be saved.

Now that we have seen what all it takes to convert an image into a different format (which isn't much, right ?), lets have a look at our image format conversion webservice.

Inside WSC_ConvertImage

Here's the code to our image conversion webservice:

<%@ WebService language="C#" class="WSC_ConvertImage" %>

Lets follow the top down approach. The code places references to required namespaces, including System.Drawing for using the Image class, System.Drawing.Imaging for using the ImageFormat enumeration, and finally, System.IO for working with files (we will see why, shortly).

Now, since this webservice has to convert an image from one format to another, it must have access to that image, and it will have to return the converted image. There are couple of issues here:

  • The webservice could be located anywhere globally, while the image could be someplace other than the webservice server.
  • We can't transfer files, as such, using webservices.
  • Once the converted image is created on the webservice server, how do we return it to the client? Refer to the second issue.

The single solution, which handles these issues gracefully, is to send the image as an array of bytes, which the webservice processes, and returns the converted image as a byte array. And this is how the ConvertImage web method has been implemented. The first parameter to the method is the byte array of the source image file. The client of the webservice is responsible for creating this byte array. The second parameter specifies the format in which the image is to be converted. Numeric values are used to specify the format, as can be seen from the switch-case construct further down the code. Finally, the third parameter is an out parameter, and is a byte array in which the converted image will be returned, and the webservice client shall create a file out of this byte array. The webmethod returns a boolean to indicate the success of the conversion.

Now that we have discussed the design implications behind the signature of the webmethod, lets understand how the webservice actually works.

How ConvertImage works?

The webmethod starts of by initializing the outbound array to null. Next, it is checked if bytes comprising the image to be converted have been sent or not. Incase not, then false is returned. Otherwise, a unique filename is generated using the NewGuid method of the Guid structure so that each webmethod call has a unique filename for processing the inptu bytes. Incase you don't know, GUIDs are 128bit numbers that are used in COM for specifying various kinds of identifiers, and are almost guaranteed to be unique.

Next, the input bytes are written to the file, created from the unqiue filename generated previosuly, and an Image object is created by loading the image using the FromFile static method. If any exception is thrown, the method returns false. Subsequently, depending upon the kind of conversion specified by the ToFormat parameter, appropriate output filename is generated, by prefixing the relevant image file extension, to the generated input filename. Next, the SaveAs method is called, with the appropriate enumeration value (corresponding to the format in which the image should be converted), and the image conversion takes place.

The final portion deals with opening and reading the output file, and transferring the bytes of the converted image file in the out byte array, that shall be sent back to the client, which will then write to a file. Finally, true is returned if everything has gone fine, to indicate a successful image conversion.

The Client

Now that we have seen how the webservice works, here's a look into a simple client for the webservice:

using System;
using System.IO;
public class TestImage
    public static void Main()
        Console.WriteLine("Enter filename: ");
        string strFileName=Console.ReadLine();
        FileStream fs=File.Open(strFileName,FileMode.Open,FileAccess.Read);
        byte[] inByte = new byte[fs.Length];

        WSC_ConvertImage wsc = new WSC_ConvertImage();
        byte[] outByte=null;
        if (wsc.ConvertImage(inByte,1,out outByte)==false)
            Console.WriteLine("Covnersion failed");
            Console.WriteLine("Conversion over"); 

The client starts of by opening the input file specified in the read mode, and reading its contents in a byte array. Next, it creates an object of the webservice, wsc, and invokes the webmethod. If an error occurs, approprate message is displayed. Otherwise, a file by the name of output.bmp is created (since the client is performing the conversion to BMP format), and the bytes sent out by the webmethod, comprising of the converted image file, are written to the created file. Hence, the conversion takes place.


Its very obvious that webservices carry a lot of punch, especially with what all they can do with the framework's support. This was a very simple exemplification of the future of distributed software services. Hope you felt the power and flexibility.


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

Kumar Gaurav Khanna
Web Developer
United States United States
I hold Early Acheiver in MCSE 2000, MCSE NT 4.0, MCP+I, and actively involved in programming using C/C++, .NET framework, C#, Win32 API, VB, ASP and MFC.

I also have various publications to my credit at MSDN Online Peer Journal, Windows Developer Journal (, Developer 2.0 (, and PC Quest (

You may also be interested in...

Comments and Discussions

Generalsave emf or wmf Pin
Anonymous18-Jun-04 23:01
sussAnonymous18-Jun-04 23:01 
GeneralRe: save emf or wmf Pin
rajeshb7929-Nov-04 4:59
memberrajeshb7929-Nov-04 4:59 
GeneralRe: save emf or wmf Pin
danbenedek9-Mar-07 3:07
memberdanbenedek9-Mar-07 3:07 
GeneralRe: save emf or wmf Pin
Partenon18-Feb-08 3:08
memberPartenon18-Feb-08 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 8 Jul 2002
Article Copyright 2002 by Kumar Gaurav Khanna
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid