Compare two Images File Using C#

Compare two Images File Using C#

In this post i m going to explain how to compare two images files using C#.
which would be very useful while performing Automation for checking whether two taken screen shot of web pages are same or not.

Below is the Code snippet which i have used for my automation purpose.

public void compareImages()
//First Image files
string Image1Location = @”C:\Pictures\For Website\DSCN0444.JPG“;
//Second Image File
string Image2Location = @”C:\Pictures\For Website\DSCN0444.JPG“;
string imageRef1, imageRef2;
//First Image BitMap
Bitmap img1Bitmap = new Bitmap(Image1Location);
//Second Image BitMap
Bitmap img2Bitmap = new Bitmap(Image2Location);
//Keep The Count of Pixel
int count1Pix = 0;
int count2Pix= 0;
//Decison whether Images are same or not
bool flag = true;
//Compare by Width
if (img1Bitmap.Width == img2Bitmap.Width)
//Compare by Height
if (img1Bitmap.Height == img2Bitmap.Height)
for (int i = 0; i < img1Bitmap.Width; i++)
for (int j = 0; j < img1Bitmap.Height; j++)
imageRef1 = img1Bitmap.GetPixel(i, j).ToString();
imageRef2 = img2Bitmap.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
flag = false;
MessageBox.Show(“Images are of Different Height”);
MessageBox.Show(“Images are of Different Width”);
catch (Exception ex)

if (flag == false)
MessageBox.Show(“Image “+Image1Location+”and “+ Image2Location+”are not same , total wrong pixel found “ + count2Pix );
MessageBox.Show(“Image ” + Image1Location + “and ” + Image2Location + “are same!! Thank you“);



3 thoughts on “Compare two Images File Using C#

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s