2

I wanted to make a color detection program to display a message box when a certain color is found

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace evade
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SearchPixel("#00042");

        }

        private bool SearchPixel(string hexcode)
        {
            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(bitmap as Image);
            graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

            Color desiredPixelColor = ColorTranslator.FromHtml(hexcode);

            while (true)
            {

                for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++)
                {
                    for (int y = 0; x < SystemInformation.VirtualScreen.Height; y++)
                    {
                        Color currentPixelColor = bitmap.GetPixel(x, y);


                        if (desiredPixelColor == currentPixelColor)
                        {

                            MessageBox.Show("Found!");
                            return true;

                        }

                        else
                        {
                            return false;
                        }
                    }



                }

            }

        }


    }
}

So that's my code. And the color is #00042 this one (html) I'm running the detection on a while loop so when the desired color appears on screen it displays a little message box but It does not work.

HellGoat77
  • 53
  • 1
  • 2
  • 7

1 Answers1

2

In your code you have:

if (desiredPixelColor == currentPixelColor)
{

    MessageBox.Show("Found!");
    return true;

}
else
{
    return false;
}

Which returns true if the color is found, but at the same time returns false as soon as a color does not match.

What happens if the very first pixel is not a match? Then you return false without ever checking the rest of the pixels!

Instead, you need to return false after the for loops have exited:

for (int x = 0; x < SystemInformation.VirtualScreen.Width; x++)
{
    for (int y = 0; x < SystemInformation.VirtualScreen.Height; y++)
    {
        Color currentPixelColor = bitmap.GetPixel(x, y);
        if (desiredPixelColor == currentPixelColor)
        {

            MessageBox.Show("Found!");
            return true;

        }
    }
}
return false;

Also, get rid of that while(true) loop as it's not necessary.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thank you very much for the Information! Also, I still think I should keep the while since I want to keep constantly checking after I click the button. – HellGoat77 Jun 08 '20 at 19:21
  • If you keep the while loop called from your button click handler, your app will become un-responsive as you'll tie up the main UI thread and it won't be able to respond to user interaction or re-paint itself. Besides, with the `return` statements in there, the while loop gets exited; thus it is doing nothing for you. If you want to periodically check, turn on a Timer control from your Button handler and then check for the color from within the Tick() event. Or create another thread and check from there... – Idle_Mind Jun 08 '20 at 21:28