Compare two Images in Asp.Net

For comparing two images take 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, 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.