Compare two Images in Asp.Net



Introduction:

This article explains how to compare two images. You also can compare images from a database. Databases do not support binary data comparison.
 
For comparing two images I'm taking both images from outside but you can replace that with database also. It requires extensive conversion like byte array to image and image to bitmap. In this article I'm comparing each pixel of a bitmap.

Image comparison is not possible in any database because the binary data may not be the same. You can try it with data length also but it will not give exact result.

Using Code:

First we convert our uploaded images into a byte array for converting this byte array into images again and then this byte array are again converted into bitmap for comparison.

    byte[] _barray1;
    byte[] _barray2;

Next we have to read the bytes of uploaded images into our byte arrays like bellow. Here you can alternatively use a database field, which 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 the images to Bitmap and pass the two bitmap to our compare method and show the 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 actual task of image comparison. This method compares each and every pixel using SHA256Managed and stores the result in enum.

public enum CompareResult
    {
        ciCompareOk,
        ciPixelMismatch,
        ciSizeMismatch
    };

    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;
        }
        else
        {
            //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;
            }
            shaM.Clear();
        }
 
        return cr;
    }

Conclusion:

This compares two images either from uploaded images or database binary data.