Comparing Two Images In Asp.Net



This article explain how you can compare two images are same or not. You can compare the images from database also. DataBases not support the binary data comparision.

For comparing two images I’m taking both of images from outside but you can replace that with database also. It’s take lots of conversion process like byte array to image and image to bitmap. In this article I’m comparing each pixel of bitmap.

Image comparision is not possible in any Database because the bynary data may not be same. You can try it with Datalength also but it will not give exact result.

Using Code:

Firstly we have convert our uploaded images into byte array for converting this byte array into images again and then this byte array are again converted into bitmap for comparision.

byte[] _barray1;

byte[] _barray2;

Next we have to read the bytes of uploaded images into our byte arrays like bellow.Here only you can go for database field also means here we can read the database binary data into our byte array. Here instead of reading bytes from Upload control you can read it from database.

//Reading Bytes From Uploaded Images


if (FileUpload1.HasFile && FileUpload2.HasFile)


using(BinaryReader reader1=new BinaryReader(FileUpload1.PostedFile.InputStream))


using (BinaryReader reader2=new BinaryReader(FileUpload2.PostedFile.InputStream))


_barray1 = reader1.ReadBytes(FileUpload1.PostedFile.ContentLength);

_barray2 = reader2.ReadBytes(FileUpload2.PostedFile.ContentLength);




Passing this two byte array to our compare method is not possible for that we have to convert this byte array to images and then this images to Bitmap and pass this two bitmap to our compare method and showing results.

//Converting Byte Array To Image And Then Into Bitmap

ImageConverter ic = new ImageConverter();

Image img = (Image)ic.ConvertFrom(_barray1);

Bitmap bmp1 = new Bitmap(img);

Image img1 = (Image)ic.ConvertFrom(_barray2);

Bitmap bmp2 = new Bitmap(img1);

//Calling Compare Function

if (Class1.Compare(bmp1,bmp2)==Class1.CompareResult.ciCompareOk)


Label1.Visible = true;

Label1.Text = “Images Are Same”;


else if (Class1.Compare(bmp1,bmp2)==Class1.CompareResult.ciPixelMismatch)


Label1.Visible = true;

Label1.Text = “Pixel not Matching”;


else if (Class1.Compare(bmp1,bmp2)==Class1.CompareResult.ciSizeMismatch)


Label1.Visible = true;

Label1.Text = “Size Is Not Same”;


The Compare Method:

This method will perform the actuall task of image comparision. This method compare each and every pixel using SHA256Managed.And stored the result in enum.

public enum CompareResult







public static CompareResult Compare(Bitmap bmp1, Bitmap bmp2)


CompareResult cr = CompareResult.ciCompareOk;


//Test to see if we have the same size of image

if (bmp1.Size != bmp2.Size)


cr = CompareResult.ciSizeMismatch;




//Convert each image to a byte array

System.Drawing.ImageConverter ic = new System.Drawing.ImageConverter();

byte[] btImage1 = new byte[1];

btImage1 = (byte[])ic.ConvertTo(bmp1, btImage1.GetType());

byte[] btImage2 = new byte[1];

btImage2 = (byte[])ic.ConvertTo(bmp2, btImage2.GetType());


//Compute a hash for each image

SHA256Managed shaM = new SHA256Managed();

byte[] hash1 = shaM.ComputeHash(btImage1);

byte[] hash2 = shaM.ComputeHash(btImage2);


//Compare the hash values

for (int i = 0; i < hash1.Length && i < hash2.Length && cr == CompareResult.ciCompareOk; i++)


if (hash1[i] != hash2[i])

cr = CompareResult.ciPixelMismatch;





return cr;



This compare the two images either from uploaded images or database binary data.

2 responses to “COMPARE TWO IMAGES

    • From your question I think so you are developing the application when any person comes with mask you want to produce the alarms. Basically this is only simple image comparing article. For your requirement you can Go to and search for FaceDetection with C#. Here you can find out so many articles related to detect the faces.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s