Click here to Skip to main content
13,548,587 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 (Sam)
Software Developer (Senior) Cherisys Technologies,
India India

Software professional with demonstrated strength in windows-based and web-based software development. Have 4 years of experience with the full software development lifecycle including requirements, design, development, testing/QA, deployment, training & support. Have 1 year experience managing groups, planning and executing implementations. Practical working knowledge of all aspects of IT. Possess strong insight into practical business considerations.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:25
membermanoj kumar choubey26-Feb-12 21:25 
Generalregarding the code provided Pin
upama thakur18-Apr-11 20:31
memberupama thakur18-Apr-11 20:31 
Thanxx 4 d beautiful article u helped me a lot in my project module......
General[My vote of 1] a few notes.. Pin
Seishin#1-Apr-10 4:19
memberSeishin#1-Apr-10 4:19 
GeneralMy vote of 1 Pin
karabax31-Mar-10 10:17
memberkarabax31-Mar-10 10:17 
GeneralRe: My vote of 1 Pin
Mohd Arshad (Sam)31-Mar-10 13:15
memberMohd Arshad (Sam)31-Mar-10 13:15 
GeneralMy vote of 1 Pin
TringXRay29-Mar-10 21:56
memberTringXRay29-Mar-10 21:56 
GeneralRe: My vote of 1 [modified] Pin
Mohd Arshad (Sam)31-Mar-10 2:30
memberMohd Arshad (Sam)31-Mar-10 2:30 
GeneralRe: My vote of 1 Pin
Mohammad Elsheimy31-Mar-10 11:31
memberMohammad Elsheimy31-Mar-10 11:31 
GeneralHi Pin
prasad0223-Mar-10 23:20
memberprasad0223-Mar-10 23: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 | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 31 Mar 2010
Article Copyright 2010 by Mohd Arshad (Sam)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid