Created
January 4, 2013 12:17
-
-
Save ChrisMcKee/4452218 to your computer and use it in GitHub Desktop.
Basic check for identical images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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