Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Visual C# » Image Comparison using C#

Image Comparison using C#

In this article, I am going to describe how to compare two images using C#.

Author Rank :
Page Views : 48485
Downloads : 2083
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageComparison.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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:

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore

I am working as a Software Developer and has 4 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
about the images by Dnyaneshwar On January 30, 2009
Hello Purushottam can you please explain me how we can make particular image only visible in form of circle(like cd,Dvd) and make certain operations on that image like inserting text on it..... above article is wonderful.... thanks for sharing..... looking forward for ur posting about my question Regards, Dnyaneshwar
Reply | Email | Modify 
I cant download user zip file by Gokul On February 24, 2009
I cant download user zip file page error is shown... plz verify...I need ur since ur article is good my id :gokul_nicefriends@rediffail.com
Reply | Email | Modify 
Re: I cant download user zip file by Purushottam On February 24, 2009
Hi gokul Once try again to download zip file.
Reply | Email | Modify 
Image Comparison C# by Attaullah On February 27, 2009
This is a great article thanx for sharing the code, there is one more thing i would like to ask and that is even though the pictures in the above example are same but you have made slight changes to the next image, is it possilble that the program compares the image and sees if the second image is 80% (percent) the same as the first image and the program should therefore accept it to be alike. Can you help me with that? Thanks AK.
Reply | Email | Modify 
Content Based Image Retrival by Gokul On February 28, 2009
Hi can any body help me to do Project LongTerm Cross session Relevance Feed back using Virtual features using c#.net
Reply | Email | Modify 
Thanks by shahid On April 9, 2009
Thanks a lot
Reply | Email | Modify 
Not a Experienced One! by Mohammad Ilyas On June 4, 2009

Image comparison is not just comparing pixels as they are.

Because two images captured by the same camera does not exactly same.

Light dimming can make all pixels unequal, so in that environment two same object

images cannot be compared with this technique, but there are so many programs in

matlab for true comparison semantically, and do not found any in C#, if you could solve this problem tell me at....

muhammadilyas_bsel@yahoo.com

thanks if you could solve...

Reply | Email | Modify 
image comparison by abdul On September 21, 2009
hi man i really liked the way you explain your program but i am new in c# and would like to know if there can be a possibility to create also a database relate to the program where image can be saved in  so that when we compare the second can take as reference the first one.
Reply | Email | Modify 
thanks by danny On November 11, 2009
thanks for sharing, although this is not a true image comparing function it does show how to access images using C#, thanks again :)

---------------------------------------------
Danny, Los Angeles Locksmith
Reply | Email | Modify 
God project! by cata On March 12, 2010
thanks man....you helped me a lot! you're cool

Reply | Email | Modify 
try image comparing using xor....... by Pawan On June 17, 2010
the code you provide is totally worst, you compare image when you perform xor. The perfect black output shows that the images are totally same.
Reply | Email | Modify 
trail verifying by venkat On June 23, 2010

so far i have not traid. after trail i will give my feed back

Reply | Email | Modify 
comparison of pixels by siddardha On August 9, 2010
its very simple and good application
Reply | Email | Modify 
comparison of pixe by siddardha On August 9, 2010
its very simple and good application
Reply | Email | Modify 
Re: comparison of pixe by Purushottam On August 10, 2010

hi siddardha

Thanks a lot....

Reply | Email | Modify 
Hello by Benjapol On August 24, 2010
Hello Purushottam can you please explain me how i can replace Red-Channel on Different pixel between 2 picture  
thanks a lot
Reply | Email | Modify 
Thank you! by Eva On August 24, 2010
Hey! Thanks for sharing.. I'h learnt new thing and I felt great! ;)
Reply | Email | Modify 
Web Cam Captured Images Comparison by C# by Jefferey On September 7, 2010
Hi.. Thanks for sharing such a good application source code. Currently, I am doing my final year project. I am having a problem in the web cam captured images comparison using C#. The object that captured by the web cam is the same object. However, the object was not accurately fall at the same position (even just a different of 0.1mm in X or Y direction) . Hence, the result from the comparison is the images are not same. In fact, what actually i want to compare is the shape of the object, to check whether the object is out of shape. I have an idea, perhaps to set a 'datum point' at the center of the object (not at the center of the image) and the comparison is started at the 'datum point.' But I was unable to write the code. Do you have any idea or solution on this matter?  Thanks in advance. :) Jefferey.
Reply | Email | Modify 
How to compare with image in database table? by Eva On September 11, 2010
Hi! Anyone has any idea on how to compare image in database table using the same method? help....
Reply | Email | Modify 
wow by Mike On February 10, 2011
this is terrible code
Reply | Email | Modify 
About image comparison by saidfix On February 14, 2011
Hi please tell me how to compare two images in web using c#???
Reply | Email | Modify 
another problem by enis On February 16, 2011
how can you compare two same image those have different color tone(for example they have different hue values)?
Reply | Email | Modify 
comment by hassin On February 19, 2011
Is a good program thank you Purushottam Rathore
Reply | Email | Modify 
image comparision by dinesh On September 14, 2011
code that you put in zip file is working.., and tell me how to do this for serial port communication .. i want to compare images that scanned by scanner kid...with the image that already stored in the data base.. for embedded proj
Reply | Email | Modify 
question by khoo On October 9, 2011
may i know what is the picture box for?
Reply | Email | Modify 
Thank's Man by soheil On November 17, 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
Reply | Email | Modify 
Clone Displays by Rajesh On December 27, 2011
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
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.