Click here to Skip to main content
Click here to Skip to main content

Program for Format Conversion of An Image

, 31 Mar 2010 CPOL
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.
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:

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")
        {
            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)

Share

About the Author

Mohd Arshad (Sam)
Software Developer (Senior) Cherisys Technologies, WebStreet.in
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.



www.cherisys.com
www.webstreet.in
www.codevdo.com
www.bizkut.com
Follow on   Twitter

Comments and Discussions

 
GeneralHi Pinmemberprasad0223-Mar-10 23:20 

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
Web02 | 2.8.141030.1 | Last Updated 31 Mar 2010
Article Copyright 2010 by Mohd Arshad (Sam)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid