这是我放在一起的一些代码(我没有写完所有这些)
Bitmap thisScreenshot = new Bitmap(Width, Height);
Graphics gfxScreenshot = Graphics.FromImage(thisScreenshot);
IntPtr hdcBitmap = gfxScreenshot.GetHdc();
PrintWindow(WindowToFind, hdcBitmap, 0);
gfxScreenshot.ReleaseHdc(hdcBitmap);
//until this is part its just getting image of window(thisScreenshot)
AForge.Imaging.Filters.ResizeBilinear filter = new AForge.Imaging.Filters.ResizeBilinear(100, 60);
Bitmap forthumbnail = filter.Apply(thisScreenshot);
pictureBox1.Image = forthumbnail;
//created thumbnail
//this is where it gets slightly interesting:
Choppa = new AForge.Imaging.Filters.Crop(new Rectangle(150,55,250,200));
Bitmap croppedACCEPTtext = Choppa.Apply(thisScreenshot);
string img1_ref, img2_ref;
Bitmap img1 = croppedACCEPTtext;
Bitmap img2 = Properties.Resources.txt305;
int count1 = 0, count2 = 0;
bool flag = true;
if (img1.Width == img2.Width && img1.Height == img2.Height)
{
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
img1_ref = img1.GetPixel(i, j).ToString();
img2_ref = img2.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
{
count2++;
flag = false;
break;
}
count1++;
}
}
if (count2 < 200)
{
//THIS IS WORKS, AND HAPPENS CORRECTLY.
//THIS IS WORKS, AND HAPPENS CORRECTLY.
}
else
{
}
}
else
{
}
//Now, remaining part above doesnt work.
//its always false, unless when I look for count2 <400, then its always true.
AForge.Imaging.Filters.Crop Choppah = new AForge.Imaging.Filters.Crop(new Rectangle(150, 55, 350, 300));
croppedACCEPTtext = Choppah.Apply(thisScreenshot);
img1 = croppedACCEPTtext;
count1 = 0;
count2 = 0;
flag = true;
img2 = Properties.Resources.txt306;
if (img1.Width == img2.Width && img1.Height == img2.Height)
{
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
img1_ref = img1.GetPixel(i, j).ToString();
img2_ref = img2.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
{
count2++;
flag = false;
break;
}
count1++;
}
}
if (count2 < 400)
{
//fail ?
}
else
{
}
}
else
{
}
这段代码不是我的,但由于某种原因它们失败了。我似乎无法理解为什么。
答案 0 :(得分:2)
你应该描述失败的意思。
如果我正确地遵循它,第一部分会:
然后我们比较相同点之间的颜色,如果它们相同则递增计数器。根据你的说法,当不超过199个像素匹配颜色时,它会成功。
(或者说不超过199个列的图像具有匹配的颜色,因为break语句不会跳出两个for循环。)
第二部分:
在相同点处比较颜色(假设图像大小相同),如果计数器相同则递增计数器。
根据你的说法,这会失败,因为img1的列少于399包含与img2相匹配的像素颜色。
您期望什么行为? (您期望有多少列具有相同颜色的位置等效像素?)
我的假设是第二部分将始终具有count2&lt; 400,因为它是通过计算具有至少1个匹配像素的图像的列数来制作的。但你通过裁剪制作了这些图像中的一个,并完成了裁剪:
new Rectangle(150, 55, 350, 300)
使新的img2看起来没有超过400列。
也许您打算使用“continue”关键字而不是“break”