Image Converter
How to Create an Image Converter in C#
Details: Image Converter
In this post, we will look into how to develop a program in C# programming language that can convert an image to a particular file extension in JPEG, PNG, BMP, or GIF. We can follow the below procedure to achieve our objective.
1 – We first create a Windows Form Application using Microsoft Visual Studio 2019
– Navigate to File
– Click on New Project
– Select Windows Application.
2 – We can add the following to our program;
– PictureBox labelled pickbox.
– Two buttons labelled btnBrowse and btnconvert.
– ComboBox labelled cbformats.
– dialogs labelled saveDialog as saveFileDialog and OFDialog as o.
You interface should be designed in this pattern
3 – Navigate to Items property of your cbformats and encode JPEG, PNG, BMP, and GIF.
4 – Insert the code below in the form_load().
public void Form1_Load(System.Object sender, System.EventArgs e)
{
cbformats.SelectedItem = "PNG";
}
5 – Input the below code in the btnbrowse to be able to browse image files.
public void btnbrowse_Click(System.Object sender, System.EventArgs e)
{
try
{
// when the Browse Image button is click it will open the OpenfileDialog
//this line of will check if the dialogresult selected is cancel then
if (OFDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
//the the selected image speciied by the user will be put into out picturebox
picbox.Image = Image.FromFile(OFDialog.FileName);
//then the image sizemode is set to strectImage
picbox.SizeMode = PictureBoxSizeMode.StretchImage;
//then we assign val in to true because val variable above is declare as boolean
val = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
6 – Insert the below code in the btnconvert to be able to convert images to its format.
public void btnconvert_Click(System.Object sender, System.EventArgs e)
{
try
{
//it check if the variable val is set to val
if (val == true)
{
//check if what format is selected by user in the combobox
//the after this the program will convert it to the desired format by the user.
if ((string) cbformats.SelectedItem == "PNG")
{
saveDialog.Filter = "PNG|*.png";
if (saveDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
else if ((string) cbformats.SelectedItem == "JPEG")
{
saveDialog.Filter = "JPEG|*.jpg";
if (saveDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
else if ((string) cbformats.SelectedItem == "BMP")
{
saveDialog.Filter = "BMP|*.bmp";
if (saveDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
else if ((string) cbformats.SelectedItem == "GIF")
{
saveDialog.Filter = "GIF|*.gif";
if (saveDialog.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
{
picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Gif);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}