我的图片位于
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();
}
}
答案 0 :(得分: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;
}