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 ImageUrl
s, 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!