Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created January 4, 2013 12:17
Show Gist options
  • Select an option

  • Save ChrisMcKee/4452218 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisMcKee/4452218 to your computer and use it in GitHub Desktop.
Basic check for identical images
namespace Utils.Extension
{
using System.Drawing;
using System.Security.Cryptography;
public static class ExtendImage
{
public static bool EqualImage(this Image img, Image imgToCompare)
{
if (img == null && imgToCompare == null)
return true;
if ((img == null && imgToCompare != null) || (img != null && imgToCompare == null))
return false;
if (img.Size != imgToCompare.Size)
{
return false;
}
else
{
//Convert each image to a byte array
var ic = new ImageConverter();
byte[] btImage1 = new byte[1];
btImage1 = (byte[]) ic.ConvertTo(img, btImage1.GetType());
byte[] btImage2 = new byte[1];
btImage2 = (byte[]) ic.ConvertTo(imgToCompare, btImage2.GetType());
//Compute a hash for each image
var shaM = new SHA512CryptoServiceProvider();
byte[] hash1 = shaM.ComputeHash(btImage1);
byte[] hash2 = shaM.ComputeHash(btImage2);
//Compare the hash values
for (int i = 0; i < hash1.Length && i < hash2.Length; i++)
{
if (hash1[i] != hash2[i])
return false;
}
return true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment