Click here to Skip to main content
13,869,102 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 23 Mar 2010
Licenced CPOL

Program for Format Conversion of An Image

, 31 Mar 2010
Rate this:
Please Sign up or sign in to vote.
.NET provides extensive support for image conversion. Any image can be processed from one format to another. The most common formats which .NET support are .BMP, .EMF, .GIF, .ICO, .JPG, .PNG, .TIF and .WMF.


Generally, a need arises to convert the format of an uploaded image to another image format, usually we provide the user freedom to upload any common image type like .jpg, .gif, .png, etc. and process all to a single format .jpg. .NET provides extensive support for such a need; any image can be processed from one format to another. The most common formats which .NET support are .BMP, .EMF, .GIF, .ICO, .JPG, .PNG, .TIF and .WMF.

Using the Code

In the demonstration program, I put two blank folders in the root directory of the solution, first to save the original images and another to save the converted images. At Default.aspx page, there is a FileUpload control through which a user can upload files of types .JPG, .PNG and .BMP and there is a DropDownList in which a user has to select the resultant image format. There is a button to show clicking on which the image has been processed to convert the format and two Image controls on page have been assigned ImageUrls, first with original image, next with resultant image.

To make the program work, first of all, one needs to add three namespaces which provide authority to use classes like ‘Path’, ‘Bitmap’, and ‘ImageFormat’. The namespaces are:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

The handler function of button which is responsible for the image to process is given below:

protected void btnResults_OnClick(object sender, EventArgs e)
    if (Page.IsValid && fpImage.HasFile)
        string tmpName = Guid.NewGuid().ToString();
        fpImage.SaveAs(MapPath("~/Original Images/" + 
		tmpName + Path.GetExtension(fpImage.FileName)));
        imgOriginal.ImageUrl = "~/Original Images/" + 
		tmpName + Path.GetExtension(fpImage.FileName);
        lblOIF.Text = Path.GetExtension(fpImage.FileName).ToString().ToUpper();
        Bitmap original_image = new Bitmap(fpImage.FileContent);
        if (drpRIF.SelectedValue == "1")
		("~/Converted Images/" + tmpName + ".jpg"), ImageFormat.Jpeg);
            imgResult.ImageUrl = "~/Converted Images/" + tmpName + ".jpg";
        else if (drpRIF.SelectedValue == "2")
            original_image.Save(MapPath("~/Converted Images/" + 
		tmpName + ".png"), ImageFormat.Png);
            imgResult.ImageUrl = "~/Converted Images/" + tmpName + ".png";
        else if (drpRIF.SelectedValue == "3")
            original_image.Save(MapPath("~/Converted Images/" + 
		tmpName + ".bmp"), ImageFormat.Bmp);
            imgResult.ImageUrl = "~/Converted Images/" + tmpName + ".bmp";
        lblRIF.Text = drpRIF.SelectedItem.Text;
        if(original_image != null) original_image.Dispose();

In the above handler function, first of all, I create a new GUID and store it in a temporary variable. The original image has now been saved with the name as GUID. This image has been assigned to an Image control on the page.

The original image has now been collected in a Bitmap class. Now after checking the required format for resultant image, the image in Bitmap has been saved with resultant image format and the newly saved image is then assigned to an Image control on the page.

Both the original image format type and the resultant image format type are mentioned along with images on the page.

Points to Ponder

The practical stuff is attached as a demo. You will easily understand when you look at the download files. Image processing operations require a high amount of server resources, if not managed properly, they can slow down the performance. It is important to release resources by disposing the objects of classes like Bitmap when you're done.

You might also be interested in the following articles that I have written:

Final Words

I hope you find the stuff helpful. Thanks for reading. Good luck!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mohd Arshad Malik
Software Developer (Senior) Cherisys Technologies
India India
Senior Software Professional with 10+ years of experience in web/desktop applications development.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 22:25
professionalManoj Kumar Choubey26-Feb-12 22:25 
Generalregarding the code provided Pin
upama thakur18-Apr-11 21:31
memberupama thakur18-Apr-11 21:31 
General[My vote of 1] a few notes.. Pin
Seishin#1-Apr-10 5:19
memberSeishin#1-Apr-10 5:19 
GeneralMy vote of 1 Pin
karabax31-Mar-10 11:17
memberkarabax31-Mar-10 11:17 
GeneralRe: My vote of 1 Pin
Mohd Arshad Malik31-Mar-10 14:15
memberMohd Arshad Malik31-Mar-10 14:15 
GeneralMy vote of 1 Pin
TringXRay29-Mar-10 22:56
memberTringXRay29-Mar-10 22:56 
GeneralRe: My vote of 1 [modified] PinPopular
Mohd Arshad Malik31-Mar-10 3:30
memberMohd Arshad Malik31-Mar-10 3:30 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy31-Mar-10 12:31
memberMohammad Elsheimy31-Mar-10 12:31 
GeneralHi Pin
prasad0224-Mar-10 0:20
memberprasad0224-Mar-10 0:20 

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 | Cookies | Terms of Use | Mobile
Web05 | 2.8.190214.1 | Last Updated 31 Mar 2010
Article Copyright 2010 by Mohd Arshad Malik
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid