In this article we will see how we can convert Images from one form to another.
Introduction
Recently I was wondering if we can change the image formats then it would be very useful for us in daily scenario
In this article we will see how we can convert the Image Formats using Windows Application.
Objective
Converting Image Formats.
Using the code
- Create a New Windows Application Project in VS 2010.
- Drag and Drop 1 Picture Box, 2 Buttons,1 Open File Dialog and 1 Save File Dialog Box and 6 Radio buttons for specifying the conversion.
- The Form should look like Screen 1
Screen 1
Working of the Application
- First we will import the namespace using System.Drawing.Imaging; as the class in Imaging will give us the facility to change the formats of the Images.
- Then we will Load the Image by clicking the Load Button and then browse the image from out computer.
- We will then select any one of the formats from the options given in the radio button.
- Once we choose the format then we will convert button.As we click the convert button a save file dialog box will open up and ask us to save the image and ask us to give it a Name.
- Once we give the name and click OK the image will be converted and saved.
- You can then view the image and check its extension.
Screen 2 :- Loading the Image
Screen 3 :- Saving the Image
Screen 4 :- Conversion Successful
Screen 5 :- Output of Image
// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace Image_Converter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// This will load the Image for conversion
private void btnLoadImage_Click(object sender, EventArgs e)
{
ofdialog.Filter = "*.jpg|*.jpg|*.bmp|*.bmp|*.gif|*.gif|*.png|*.png";
if (ofdialog.ShowDialog() == DialogResult.OK)
{
pbOgImage.Image = Image.FromFile(ofdialog.FileName);
}
}
// This will set the extension while conversion
private string AssignExtensionFilter()
{
if (rdJpeg.Checked) return "*.jpg|*.jpg";
if (rdgif.Checked) return "*.gif|*.gif";
if (rdpng.Checked) return "*.png|*.png";
if (rdbmp.Checked) return "*.bmp|*.bmp";
if (rdICO.Checked) return "*.ico|*.ico";
if (rdExif.Checked) return "*.exif|*.exif";
return string.Empty;
}
// The Imaging class will help to convert the image and save it as the desired format
private void btnConvert_Click(object sender, EventArgs e)
{
Image convertedImage = pbOgImage.Image;
sfdialog.Filter = AssignExtensionFilter();
if (sfdialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (rdJpeg.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Jpeg);
MessageBox.Show("Conversion Successfull");
}
else if (rdgif.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Gif);
MessageBox.Show("Conversion Successfull");
}
else if (rdpng.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Png);
MessageBox.Show("Conversion Successfull");
}
else if (rdbmp.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Bmp);
MessageBox.Show("Conversion Successfull");
}
else if (rdICO.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Icon);
MessageBox.Show("Conversion Successfull");
}
else if (rdExif.Checked)
{
convertedImage.Save(sfdialog.FileName, ImageFormat.Exif);
MessageBox.Show("Conversion Successfull");
}
}
else
{
MessageBox.Show("Image is not Valid");
}
}
}
}
Explanation of AssignExtensionFilter() Function
This function will set the extension on the base of the radio button selected by the user.
Conclusion
The main idea of writing this article is to give user a home made conversion app for everyday use.