SIGN UP MEMBER LOGIN:    
ARTICLE

Image Comparison using C#

Posted by Purushottam Rathore Articles | Visual C# January 02, 2009
In this article, I am going to describe how to compare two images using C#.
Reader Level:
Download Files:
 

I am proud to present 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#.

Lets 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.

private
void Form1_Load(object sender, EventArgs e)
{
    progressBar1.Visible =
false;
    pictureBox1.Visible =
false;
}

Step 5: Add using System.IO; the namespace.

Step 6: Write the following line on the linkLabel1_LinkClicked event

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    openFileDialog1.FileName =
"";
    openFileDialog1.Title =
"Images"
    openFileDialog1.Filter =
"All Images|*.jpg; *.bmp; *.png"
    openFileDialog1.ShowDialog(); 
    if (openFileDialog1.FileName.ToString() != "")
    {
        fname1 = openFileDialog1.FileName.ToString();
    }
}

Step 7: Write the following line on the linkLabel2_LinkClicked event

private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    openFileDialog2.FileName =
"";
    openFileDialog2.Title =
"Images"
    openFileDialog2.Filter =
"All Images|*.jpg; *.bmp; *.png"
    openFileDialog2.ShowDialog(); 
    if (openFileDialog2.FileName.ToString() != "")
    {
        fname2 = openFileDialog2.FileName.ToString();
    }
}

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

private void button1_Click(object sender, EventArgs e)
{
    progressBar1.Visible =
true;
    string img1_ref, img2_ref;
    img1 =
new Bitmap(fname1);
    img2 =
new Bitmap(fname2);
    progressBar1.Maximum = img1.Width;
    if (img1.Width == img2.Width && img1.Height == img2.Height)
    {
        for (int i = 0; i < img1.Width; i++)
        {
            for (int j = 0; j < img1.Height; j++)
            {
                img1_ref = img1.GetPixel(i, j).ToString();
                img2_ref = img2.GetPixel(i, j).ToString();
                if (img1_ref != img2_ref)
                {
                    count2++;
                    flag =
false;
                    break;
                }
                count1++;
            }
            progressBar1.Value++;
        }
        if (flag == false)
        MessageBox.Show("Sorry, Images are not same , " + count2 + " wrong pixels found");
        else
        MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");
    }
    else
    MessageBox.Show("can not compare this images");
    this.Dispose();
}

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:

Login to add your contents and source code to this article
share this article :
post comment
 

How complicated is it to do a project like this as a Web based application ?

Posted by aravind sanker Mar 19, 2012

can u tell me how to find an image1 which is a part of image 2 in image 2. fr eg. image 1= swing image 2= tree with swing after running the code , result must be 'position of image 1 in image 2'..

Posted by sonia naidu Mar 19, 2012

Thanks for this work I`m begining in this and i hope learn more.

Posted by Eleazar Mtz Feb 17, 2012

Hello Rathore, Could you Pls help me regarding this issue? I want to compare two screen (CRT+HDMI) and check for the junk display while cloning the display. Plz help me my mail id :rajesh.nisarga@gmail.com

Posted by Rajesh Dec 27, 2011

You're look so cool , Thank You. One Question If Possible : In FingerPrint , I do ImageProcessing With Mathlab on one image(FingerPrint) and I want to compare this image with image that exist in swl database, if your program can help me and it's accuracey is high for fingerprint compiration. Thank You

Posted by soheil sedighi Nov 17, 2011
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor