1      Introduction

Today we are looking at graphic functions from zooming, to cropping, rotating, image detection, colour correction and anything else you can think of! Again, this is going to be a series of samples so come back often to see if a new episode has been released.

2      Building the Sample

The sample is built in Visual Studio 2012 Ultimate as an x86 targeted application using .Net Framework 4. We will be using NuGet packages, a number of 3rd party libraries. All of which will be fully explained to you ensuring that the final compilation of your application will be hassle free. Oh! And the sample code is verbosely commented so you should have no problem in working out what the code does.

3      Let’s Get Started!

Open Visual Studio 2012 and create a new windows forms application. I am will be commenting in this sample using C# but a VB.NET version of the code is available for downloading as well!

Before we start adding a lot of image conversion functions to our application we need to create our basic application GUI. This will be increased and improved upon as we continue through the series. Our app currently looks like this:

 

We have an image loader with an image drop down list as well as an image viewer on the right which is scrollable and zoomable. We did not create this but adopted a third-party viewer because re-inventing the wheel is the most stupid thing a software developer can waste their time on. This is the features that the PictureBox already offers:

 

We currently only have two methods in our code one loads a new image and places it in the listbox for later re-selection. And the other is the ListBox SelectedIndexChanged event which loads the image that the user selects from the listbox.

4      btnLoadNewImage_Click(object sender, EventArgs e)

C#
Edit|Remove
private void btnLoadNewImage_Click(object sender, EventArgs e) 
              { 
                     DialogResult dr = openFile.ShowDialog(); 
 
                     if (dr == System.Windows.Forms.DialogResult.OK) 
                     { 
                           foreach (string file in openFile.FileNames) 
                           { 
                                 listBox1.Items.Add(file); 
                                  pbImage.Image = Image.FromFile(file); 
                           } 
                     } 
              }
 

All this method does is shows an OpenFileDialog when the button beside the listbox is clicked. It loads one or more files into the listbox and displays that image in the PictureBox.

5      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

C#
Edit|Remove
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
              { 
                     if (listBox1.SelectedIndex > ilDefault.Images.Count - 1// Must be added by the user 
                     { 
                           pbImage.Image = Image.FromFile(listBox1.SelectedItem.ToString()); 
                     } 
                     else // Must be the default images in the ImageList 
                     { 
                           pbImage.Image = ilDefault.Images[listBox1.SelectedIndex]; 
                     } 
              } 

5.1     Source Code Files

5.2    More Information

To convert the solution to a previous version of Visual Studio you can use this free application: http://vsprojectconverter.codeplex.com/ or download and use Visual Studio 2013 Express which is freely available from Microsoft from here: http://www.visualstudio.com/downloads/download-visual-studio-vs.

Cyotek.Windows.Forms.ImageBox.dll (http://cyotek.com/blog/imagebox-update-version-1-1-0-0)

The Visual Basic version of this code was generated by the free tool: Instant VB from http://www.tangiblesoftwaresolutions.com/