位图检测 [英] Bitmap Detection
问题描述
我现在有一个code,将在本计划采取截图的位图进行搜索,然而,位图存在的截图三次,我希望它点击它发现它的第二次。
I currently have a code that will search for a bitmap in a screenshot taken by this program, however, the bitmap exists three times in the screenshot and I want it to click the second time it finds it.
有没有办法做到这一点?提前很多感谢的......
Is there any way to do this? A lot of thanks in advance...
code:
private Bitmap Screenshot()
{
Bitmap bmpScreenShot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmpScreenShot);
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
return bmpScreenShot;
}
private bool FindBitmap(Bitmap BmpNeedle, Bitmap BmpHaystack, out Point location)
{
for (int outerX = 0; outerX < BmpHaystack.Width - BmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < BmpHaystack.Height - BmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < BmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < BmpNeedle.Height; innerY++)
{
Color cNeedle = BmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = BmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
continue;
}
}
}
location = new Point(outerX, outerY);
return true;
}
}
location = Point.Empty;
return false;
}
public void findImage()
{
Bitmap bmpScreenshot = Screenshot();
Point location;
bool success = FindBitmap(Properties.Resources.xxx, bmpScreenshot, out location);
}
不知道是不是真的有帮助,我想要它做的就是点击它找到的第二位。
Don't know if that really helps, all I want it to do is click the second bitmap it finds.
有一个朋友也建议我分裂成截图电网和这样做的原因,是做网格的方式去还是有可能找到第二位?
A friend did suggest splitting my screenshot into grids and doing it that why, is doing grids the way to go or is it possible to find the second bitmap?
更新:说我有完全相同的图像5我的屏幕上。我想我的程序点击第三位找到。
Update: Say I have 5 of the the exact same images on my screen. I want my program to click the third bitmap it finds.
推荐答案
做的是推出一个计数器,显示图片多少次发现的最简单方法。不知怎的,像这样的:
Easiest way to do would be to introduce a counter that shows how many times the picture was found. Somehow like this:
private bool FindBitmap(Bitmap BmpNeedle, Bitmap BmpHaystack, out Point location)
{
int countTimesFound = 0;
for (int outerX = 0; outerX < BmpHaystack.Width - BmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < BmpHaystack.Height - BmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < BmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < BmpNeedle.Height; innerY++)
{
Color cNeedle = BmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = BmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
continue;
}
}
}
countTimesFound++;
if (countTimesFound == 2)
{
location = new Point(outerX, outerY);
return true;
}
}
}
location = Point.Empty;
return false;
}
虽然你真的应该考虑进行图像检测技术。有迹象表明,允许这样做更容易库。
Though you should really look into techniques for image detection. There are libraries that allow doing that easier.
这篇关于位图检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!