Image Comparison in C#

The code sample is written in C#. It is a windows forms application that checks if two images are the same. This is done by first checking their height and width. The process is continued only when the two parameters are equal for the images. If so, each pixel in one image is compared to the corresponding pixel in the other.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication2
public partial class Form1 : Form
public Form1()
static string fname1,fname2;
Bitmap img1, img2;
int count1=0,count2=0;
bool flag = true;

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
openFileDialog1.FileName = ""; // Setting the opefiledialog to default
openFileDialog1.Title = "Images"; // Title of the dialog
openFileDialog1.Filter = "All Images|*.jpg; *.bmp; *.png"; // In the filter you should write all the types of image

if (openFileDialog1.FileName.ToString() != "")
fname1 = openFileDialog1.FileName.ToString();


private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
openFileDialog2.FileName = ""; // Setting the opefiledialog to default
openFileDialog2.Title = "Images"; // Title of the dialog
openFileDialog2.Filter = "All Images|*.jpg; *.bmp; *.png"; // In the filter you should write all the types of image

if (openFileDialog2.FileName.ToString() != "")
fname2 = openFileDialog2.FileName.ToString();

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)
flag = false;



if (flag == false)
MessageBox.Show("Sorry, Images are not same , " + count2+ " wrong pixels found");
MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");
MessageBox.Show("can not compare this images");



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



  • Image Compare in C# (19811-26215-IMAGEDETECTIVE1.1.msi)
  • Comments

    Guest Author: vishal30 Mar 2012

    thanks a lot. its anice article. through this article I am able to do comparison. and i think its a fastest way to compare two images. but one more thing i hve to mark the diiference between the two imges either by drawing a rectangle or a circle. plz help me.

    Guest Author: Prathap24 Jan 2014

    am going to develop a project inn this article alert on this comparing images.. will u help me pls.. i want the right guidence from you.. how can i contact you..

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: