简体   繁体   English

如何使用哈希字节比较2个图像是否相同?

[英]How do I compare if 2 images are the same using Hash bytes?

private void button1_Click(object sender, EventArgs e)
{
    Bitmap im1 = new Bitmap(@"C:\Users\user\Downloads\CaptchaCollection\1.png");
    Bitmap im2 = new Bitmap(@"C:\Users\user\Downloads\CaptchaCollection\2.png");

    if (HashImage(im1) == HashImage(im2))
    {
        MessageBox.Show("Same Image");
    }

    else
    {
        MessageBox.Show("Different Image");
    }
}

If the button is clicked it will compare these 2 images. 如果单击该按钮,它将比较这两个图像。

Here is the code that is used to hash an image. 这是用于散列图像的代码。

public byte[] HashImage(Bitmap image)
{
    var sha256 = SHA256.Create();

    var rect = new Rectangle(0, 0, image.Width, image.Height);
    var data = image.LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat);

    var dataPtr = data.Scan0;

    var totalBytes = (int)Math.Abs(data.Stride) * data.Height;
    var rawData = new byte[totalBytes];
    System.Runtime.InteropServices.Marshal.Copy(dataPtr, rawData, 0, totalBytes);

    image.UnlockBits(data);

    return sha256.ComputeHash(rawData);
}

So how do I use the HashImage() method to compare both those images if they're the same visually or not? 那么我如何使用HashImage()方法来比较这些图像,如果它们在视觉上是否相同?

I tried comparing 2 images that are clearly the same but they aren't working to compare correctly. 我尝试比较两个明显相同的图像,但它们无法正确比较。 Instead I'm getting as if it's a different image. 相反,我觉得它是一个不同的形象。

I even tried this but it's not working either. 我甚至试过这个,但它也没有用。

if (HashImage(im1).Equals(HashImage(im2)))

UPDATE: I've tried this but it isn't working either. 更新:我试过这个,但它也没有用。

if (ReferenceEquals(HashImage(im1),HashImage(im2)))

I know 3 ways to compare byte array: 我知道比较字节数组的3种方法:

  • byte[].SequenceEqual(byte[]) 字节[]。SequenceEqual(字节[])
  • System.Text.Encoding.UTF8.GetString(byte[]) == System.Text.Encoding.UTF8.GetString(byte [])==
  • Convert.ToBase64String(byte[]) == Convert.ToBase64String(byte [])==

For your code you can easy try this: 对于您的代码,您可以轻松尝试:

   Console.WriteLine("SEQUENCE EQUAL: " + (HashImage(im1).SequenceEqual(HashImage(im2)) ? "TRUE" : "FALSE") + " (easiest way)");
   Console.WriteLine("UTF8 STRING:    " + (System.Text.Encoding.UTF8.GetString(HashImage(im1)) == System.Text.Encoding.UTF8.GetString(HashImage(im2)) ? "TRUE" : "FALSE") + " (conversion to utf string - not good for display or hash, good only for data from UTF8 range)");
   Console.WriteLine("HASH STRING:    " + (Convert.ToBase64String(HashImage(im1)) == Convert.ToBase64String(HashImage(im2)) ? "TRUE" : "FALSE") + " (best to display)");

   Console.WriteLine("1: " + Convert.ToBase64String(HashImage(im1)));
   Console.WriteLine("2: " + Convert.ToBase64String(HashImage(im2)));

Add this to your code right after initialization of Bitmap im2, and look at results in output window. 在初始化Bitmap im2之后立即将其添加到您的代码中,并在输出窗口中查看结果。 You can use any of this for compare and evaluate if array is the same. 您可以使用其中任何一个进行比较,并评估数组是否相同。

Note : System.Text.Encoding.UTF8.GetString is not suitable for use in this case (hash data from picture). 注意System.Text.Encoding.UTF8.GetString不适合在这种情况下使用(图片中的哈希数据)。 See comment from @CodesInChaos below. 请参阅下面@CodesInChaos的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM