Lossless Image Converting in C# - First Things First
(Page 2 of 4 )
The first thing is the third habit of highly effective people as described in Stephen Covey's book. It has been said that it applies to every area of our lives. That's true and here we can also include programming. For that reason we fire up our Visual Studio and start a new Windows Application in C#. Then we add the following namespaces:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
We design and architect our form so it looks the fanciest with the components we require. That is, a PictureBox for a preview thumbnail, a few Buttons (Select, Quit, Convert), a ComboBox where we include the available extensions for conversion, an OpenFileDialog and SaveFileDialog, and finally we add the appropriate label(s).
Check out the way my form looks. Please pay attention to how I have additionally noted the name of each component with the arrow. This way you can follow along during this tutorial while I showcase my code snippets as well as my explanations when I'm referring to particular components. So please design your form appropriately.

(Form Design - Screenshot)
Now that we have architected our form we can continue. Although the above screenshot doesn't show the OpenFileDialog and SaveFileDialog, we shouldn't forget to add those components. I have named them SelectPicture and saveFileDialog1, respectively. Once those are also created, let's move on!
Here are a few additional details regarding our main form. I have named it clsMain. Its exact size is (455, 318) - (width, height). I have disabled the maximizeBox and the FormBorderStyle is set to FixedSingle. Its title is visible from my screenshot: Lightweight Image Manipulation. Now let's fasten our seat belts and speed up!
Check out the attached code snippet. With these we jump-start our application.
public clsMain()
{
InitializeComponent();
}
static void Main()
{
Application.Run(new clsMain());
}
private void clsMain_Load(object sender, EventArgs e)
{
this.lbl_extension.Hide();
this.ComboBox1.Hide();
this.btnConvert.Hide();
SelectPicture.Filter = "All Image files|*.bmp; *.gif; *.jpg; *.ico; " + "*.emf; *.wmf|Bitmap Files (*.bmp; *.gif; *.jpg; " + "*.ico)|*.bmp; *.gif; *.jpg; *.ico|" + "Meta Files (*.emf; *.wmf; *.png)|*.emf; *.wmf; *.png";
}
Keep in mind that some of these might have been automatically generated by the Windows Form Designer, so just recheck their existence [here I'm referring to clsMain() and Main()]. As you can notice from the above block of code, we hide the components we don't need to see now until the image isn't selected (please note that we don't hide the btnSelectPicture!). We also configure the filter of SelectPicture for images.
Next: The Real Thing >>
More C# Articles
More By Barzan "Tony" Antal