0

I want to compare the color of the pixel in the middle of the screen in realtime to colors of a picture, and start a certain action when they overlap.

The Problem is that the pictures I want to compare the color with have way too many different colors and nuances, giving them each a different color code.

As a result, it is nearly impossible to get an accurate and consistent result without listing hundreds or thousands of colors for comparison (which would probably make the code really slow).

This is what I got until now, I have no idea how to solve this basic problem. The rest of the code is probably also pretty bad, so sorry; I´m really new to programming. Thanks for every answer!

static void Main(string[] args)
{
    while (true)
    {
        List<string> givenList = new List<string> { "#FEFEFE", "#000000" };
        //insert as many colors as you want, here white and black

        Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height); 
        Graphics graphics = Graphics.FromImage(bitmap as Image);
        graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
        Color middleCol = bitmap.GetPixel(SystemInformation.VirtualScreen.Width / 2, SystemInformation.VirtualScreen.Height / 2); //get the color value of only the middle Pixel and turn it into a string, so it can be compared to the list
        string currentColor = ColorTranslator.ToHtml(middleCol);
        Console.WriteLine("Current:" + currentColor);
        Thread.Sleep(50);
        foreach (string s in givenList) //do a certain action if the values overlap
        {
            if(s == currentColor)
            {
                //do whatever
            }
        }
    }
}
Mohamad Mousheimish
  • 1,641
  • 3
  • 16
  • 48
  • 2
    For a discussion on Color Distance see [here](https://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/27375621#27375621). What is the real goal? Maybe explaining it would make the question clearer.. – TaW Jun 26 '20 at 14:48
  • 1
    I want to write a Bot for an offline video game, which recognizes, when certain game-monsters pass the screen (hence the middle of the screen in this case), and then perform an action (like a simple click, I´m not sure about that yet). However, these monster-graphics are influenced by shadows and have a lot of colors, so I want to find a way to still recognize them (the background also constantly changes, so I can´t just write something do act "when the color changes"). – GregoryGree Jun 26 '20 at 14:58
  • You will have to analyze the hue range of the monsters and test if the pixel falls in a range. Color.GetHue may help..Also: If you only want to compare 1 pixel you should also only grab one. – TaW Jun 26 '20 at 15:31
  • Thank you! Is there any way to create a Bitmap or Graphic of ONLY the middle pixel, to make it faster? Otherwhise I would have to Bitmap the whole Screen everytime, just to get the one pixel in the middle. – GregoryGree Jun 26 '20 at 18:48
  • [Sure](https://stackoverflow.com/questions/1483928/how-to-read-the-color-of-a-screen-pixel) – TaW Jun 26 '20 at 19:25

0 Answers0