2

So here is my code:

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

namespace GandomTest
{
    class Program
    {
        public static 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);

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

                    if (desiredPixelColor == currentPixelColor)
                    {

                        Console.WriteLine("Color found!");
                        Thread.Sleep(10000);
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("Not found");
                        continue;
                    }
                }
            }

            return false;
        }

        static void Main(string[] args)
        {
            SearchPixel("#000000");
            Console.ReadKey();
        }
    }
}

I was trying to loop the pixel recognition so as soon as the pixel is detected write something in console. The thing I do not know how can I make this work properly. I know this probably isn't the fastest way to do it but everytime I run it, and the specified color is on screen, the program does not detect it.

Jozese
  • 33
  • 4
  • What happens when you step through the code with the debugger? It should tell you everything you need to know. – LarsTech Jun 23 '20 at 18:59
  • 2
    Why do you switch from Screen.PrimaryScreen.Bounds to SystemInformation.VirtualScreen? Use the debugger to see what the virtual screen parameters are... – Jay Buckman Jun 23 '20 at 19:19
  • Check [this](https://stackoverflow.com/a/60144303/10216583) out. Might help. –  Jun 23 '20 at 21:44
  • @JQSOFT Could you provide the visual studio project? It seems really useful to me but I can't get it to work. Thanks by the way! – Jozese Jun 23 '20 at 22:19
  • @JQSOFT But my point is to search any color within the entire screen, not just one certain point. – Jozese Jun 24 '20 at 01:13
  • Yes that what it does if you remove the mentioned line. Have you tried? Just search for the color: `if (Color.FromArgb(alpha, red, green, blue).ToArgb().Equals(targetColor.ToArgb()) { //color exists.... }` –  Jun 24 '20 at 01:24
  • Your code works fine here – TaW Jun 24 '20 at 08:58
  • 1
    @JQSOFT Oh okay, It seems to work now. But when referring to `var targetColor =...` What's the Color code I need to input? Hex color code? – Jozese Jun 24 '20 at 10:27
  • Hello there. Should be a color like `targetColor = ColorTranslator.FromHtml(hexcode);` or `targetColor = Color.FromArgb(23, 93, 233);` Just of a Color structure from any source. –  Jun 24 '20 at 12:57
  • @JQSOFT Okay thanks, it works flawlessly but now, if I wanna keep scanning continuously after clicking the button, do I just loop it with ´ while(true) ´ or do I need to use a timer? – Jozese Jun 24 '20 at 16:21
  • Why would you do that? Do you mean searching for more that one color? If so, just create a list of colors: `var targetColors = new List();`, add some colors to find, and just replace the `if` block with `if (targetColors.Any(c => c.ToArgb().Equals(Color.FromArgb(alpha, red, green, blue).ToArgb()))) { //A color exists }` –  Jun 24 '20 at 16:33
  • @JQSOFT No, I mean, let's say I'm waiting a random amount of time until a color appears.Then once it finds it, which it does, the program stops running. I want it to keep scanning, I thought I could do it with while(true) and so did I. But the program crashes after finding the color. – Jozese Jun 24 '20 at 16:57
  • I tell you what. Edit your question to add what have you tried so far in a new `code block` to be on the same page. Also it will be useful to mention the finale result that you are trying to get. –  Jun 24 '20 at 17:13

0 Answers0