Click here to Skip to main content
15,878,814 members
Articles / Web Development / ASP.NET

Program for Format Conversion of An Image

Rate me:
Please Sign up or sign in to vote.
4.40/5 (18 votes)
31 Mar 2010CPOL2 min read 46.5K   2K   33   9
.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.
snapshot.jpg

Introduction

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:

C#
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:

C#
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")
        {
            original_image.Save(MapPath
		("~/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!

License

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


Written By
Technical Lead Cherisys Technologies
India India
Senior Software Professional with 13+ years of experience in web/desktop applications development.

Comments and Discussions

 
GeneralMy vote of 1 Pin
karabax31-Mar-10 10:17
karabax31-Mar-10 10:17 
GeneralRe: My vote of 1 Pin
Mohd Arshad Malik31-Mar-10 13:15
Mohd Arshad Malik31-Mar-10 13:15 

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.