-3

我有一张图片位于

C:\ImageOne.png

我在目录中有 20 张图片

C:\Images

如何将 ImageOne.png 与所有这些图像进行比较?

例子:

想象一下,我将一张 reCaptcha 图像保存为 C:\ImageOne.png

在位于 C:\Images 的文件夹中,我还有其他 reCaptcha 图像。

然后我需要一个可以在 C:\Images 中找到相同图像的代码

当前代码:

public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
{
    MemoryStream ms = new MemoryStream();
    firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String firstBitmap = Convert.ToBase64String(ms.ToArray());
    ms.Position = 0;

    secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    String secondBitmap = Convert.ToBase64String(ms.ToArray());

    if (firstBitmap.Equals(secondBitmap))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Bitmap img2 = new Bitmap(@"C:\ImageOne");

private void CheckCaptcha()
{
    foreach (string s in Directory.GetFiles(@"C:\Images"))
    {
        Bitmap img1 = new Bitmap(s);

        if (ImageCompareString(img1, img2) == true)
        {
            Logging("Identical");
        }
        else
        {
            Logging("Not Identical");
        }
        img1.Dispose();
    }
}
4

1 回答 1

0

也许这会有所帮助:

Bitmap img2 = new Bitmap(@"C:\ImageOne.png");
ImageConverter converter = new ImageConverter();
byte[] img2Bytes = (byte[])converter.ConvertTo(img2, typeof(byte[]));

foreach (string s in Directory.GetFiles(@"C:\Images"))
{
    Bitmap img1 = new Bitmap(s);
    byte[] img1Bytes = (byte[])converter.ConvertTo(img1, typeof(byte[]));
    if (CompareImages(img1Bytes,img2Bytes))
    {
        Logging("Identical");
    }
    else
    {
        Logging("Not Identical");
    }
    img1.Dispose();
}

public bool CompareImages(byte[] b1, byte[] b2)
{
   EqualityComparer<byte> comparer = EqualityComparer<byte>.Default;
   if (b1.Length == b2.Length)
   {
       for (int i = 0; i < b1.Length; i++)
       {
           if (!comparer.Equals(b1[i], b2[i])) return false;
       }
    } else { return false; }

     return true;
}
于 2013-12-28T22:14:50.460 回答