Image Comparison using C#

I am proud to present to you our unique image comparison application in C#.Net. C# .Net provides a good support for processing on the image, and the purpose of this article is not to give you a lot of insight into the image processing, rather it is written to help you start your image processing career using C#.

Let's create a project and compare two images using this application. to do this follows the given steps:

Step 1: In Visual studio 2005. click on file menu -> New -> Project then the following dialogbox will appear.

Image1.JPG

Figure 1:

Step 2: Now drag and drop the following tools on the Form from toolbox.

Two LinkLable
One Button
Two OpenFileDialog
ProgressBar
PictureBox

Image2.JPG

Figure 2:

Step 3: Go to properties window and design the form as you want.

Image3.JPG

Figure 3:

Step 4: Double click on form and write the following line on the form_load.

  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     progressBar1.Visible = false;  
  4.     pictureBox1.Visible = false;  
  5. }  
Step 5: Add namespace
  1. using System.IO;

Step 6: Write the following line on the linkLabel1_LinkClicked event

  1. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  2. {  
  3.     openFileDialog1.FileName = "";  
  4.     openFileDialog1.Title = "Images";   
  5.     openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png";   
  6.     openFileDialog1.ShowDialog();   
  7.     if (openFileDialog1.FileName.ToString() != "")  
  8.     {  
  9.         fname1 = openFileDialog1.FileName.ToString();  
  10.     }  
  11. } 

Step 7: Write the following line on the linkLabel2_LinkClicked event

  1. private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)  
  2. {  
  3.     openFileDialog2.FileName = "";  
  4.     openFileDialog2.Title = "Images";   
  5.     openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png";   
  6.     openFileDialog2.ShowDialog();   
  7.     if (openFileDialog2.FileName.ToString() != "")  
  8.     {  
  9.         fname2 = openFileDialog2.FileName.ToString();  
  10.     }  
  11. } 

Step 8: Finally write the follwoing code in the button click event.

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     progressBar1.Visible = true;  
  4.     string img1_ref, img2_ref;  
  5.     img1 = new Bitmap(fname1);  
  6.     img2 = new Bitmap(fname2);  
  7.     progressBar1.Maximum = img1.Width;  
  8.     if (img1.Width == img2.Width && img1.Height == img2.Height)  
  9.     {  
  10.         for (int i = 0; i < img1.Width; i++)  
  11.         {  
  12.             for (int j = 0; j < img1.Height; j++)  
  13.             {  
  14.                 img1_ref = img1.GetPixel(i, j).ToString();  
  15.                 img2_ref = img2.GetPixel(i, j).ToString();  
  16.                 if (img1_ref != img2_ref)  
  17.                 {  
  18.                     count2++;  
  19.                     flag = false;  
  20.                     break;  
  21.                 }  
  22.                 count1++;  
  23.             }  
  24.             progressBar1.Value++;  
  25.         }  
  26.         if (flag == false)  
  27.         MessageBox.Show("Sorry, Images are not same , " + count2 + " wrong pixels found");  
  28.         else  
  29.         MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");  
  30.     }  
  31.     else  
  32.     MessageBox.Show("can not compare this images");  
  33.     this.Dispose();  
  34. } 

Step 9: Now debug the application. When you compare same images like as follows:

Img1.jpg

Img 1:

Img2.jpg

Img 2:

Then the following output will occur.

Image4.JPG

Figure 4:

Step 10: When you compare different images like as follows:

Img3_Different.jpg

Img 3_Different:

Then the following output will occur.

Image5.JPG

Figure 5:


Similar Articles