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?
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.