Blue Theme Orange Theme Green Theme Red Theme
 
HeaderAd
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 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:
Technologies: .NET 1.0/1.1, .NET 2.0, ASP.NET 1.0, ASP.NET 2.0,Visual C# .NET
Total downloads : 258
Total page views :  5621
Rating :
 3.25/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ImageComparison.zip
 
ArticleAd
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
 [Top] Rate 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, SQL Server 2005, C#.Net, AJAX and .NET.
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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
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.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ImageComparison.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
about the imagesDnyaneshwar1/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 fileGokul2/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 filePurushottam2/24/2009
Hi gokul Once try again to download zip file.
Reply | Email | Delete | Modify | 
Image Comparison C#Attaullah2/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 RetrivalGokul2/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 | 
Thanksshahid4/9/2009
Thanks a lot
Reply | Email | Delete | Modify | 
Not a Experienced One!Mohammad Ilyas6/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 | 

 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
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved