I want to make color_detection_bot and This bot provide me that when I click the button in this form, mousecursor changes position to facebook login button and click it.First of all I created a bitmap in Paint and save in System.Resources name as bmpLogin and this bitmap shows a 24 bit bitmap of facebook login button.
private void btn_login_Click(object sender, EventArgs e)
{
this.BackgroundImage = Screenshot(); //Method which is take screenshot
Point location;
bool success=FindBitmap(Properties.Resources.bmpLogin, Screenshot(), out location);
if (success == false)
{
MessageBox.Show("Couldn't find the login button");
return;
}
Cursor.Position = location; //bitmap location within screenshot
MouseCLick(); //Mouse click event works successfull
}
/// <summary>
/// Find the location of bitmap within another bitmap and return if it was succesfully found
/// </summary>
/// <param name="bmpNeedle"> The image we want to find </param>
/// <param name="bmpHaystack"> Where we want to search for the image </param>
/// <param name="location"> Where we found the image </param>
/// <returns> If the bmpNeedle was found succesfully </returns>
private Boolean FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
for (int outerX = 0; outerX < bmpHaystack.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.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)
{
goto Notfound;
}
}
}
location = new Point(outerX, outerY);
return true;
Notfound:
continue;
}
}
location = Point.Empty;
return false;
}
This codes works perfectly But When I change if statemant as shown in below, Bot doesnt work.Where am I doing logical error
private Boolean FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
for (int outerX = 0; outerX < bmpHaystack.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.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)
{
location = new Point(outerX, outerY);
return true;
}
}
}
}
}
location = Point.Empty;
return false;
}