Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
World Class ASP.NET Hosting – Click Here for 3 Months Free/NO Setup Fee!
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET & Web Forms » 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:
Total page views :  13016
Total downloads :  597
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageComparison.zip
 
Become a Sponsor


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
 About the author
 
Purushottam Rathore
I am working as a Software Developer and has around 2+ years of experience in the IT industry 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.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
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.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ImageComparison.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | 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 | Delete | Modify | 
Thanks by shahid On April 9, 2009
Thanks a lot
Reply | Email | Delete | 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 | Delete | Modify | 
needing help on algorithm to be include in the image comparison by Noor Aznimah On September 2, 2009
hi Purushottam, thanks a lot for the great post. i'm not really well in this language. i have a project that need me to compare for the image comparison using the algorithm. can you help me, i would like to give you more details on my project plus the algorithm use. i really need your help, can i private message with you since this is for my master's project. and i need to elaborate more. my email : aznimahaziz@yahoo.com
Reply | Email | Delete | 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 | Delete | Modify | 
Dont DO in Feture by Nasir On October 10, 2009
http://www.dotnetspider.com/resources/19811-Image-Compare-C.aspx
Your code found from this Link
So why u up this code
Reply | Email | Delete | 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 | Delete | Modify | 
God project! by cata On March 12, 2010
thanks man....you helped me a lot! you're cool

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.