How would I be able to find a certain pixel on any screen under a certain RGB, and then get its x y coordinants?


Thank you!

Recommended Answers

All 8 Replies

This compares ARGB but you could change it to just use RGB:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmScreenShot : Form
  {
    public frmScreenShot()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Point[] points = FindColor(button1.BackColor);
      System.Diagnostics.Debugger.Break();
    }

    private static Bitmap GetScreenShot()
    {
      Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
      {
        using (Graphics gfx = Graphics.FromImage(result))
        {
          gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
        }
      }
      return result;
    }

    private static Point[] FindColor(Color color)
    {
      int searchValue = color.ToArgb();
      List<Point> result = new List<Point>();
      using (Bitmap bmp = GetScreenShot())
      {
        for (int x = 0; x < bmp.Width; x++)
        {
          for (int y = 0; y < bmp.Height; y++)
          {
            if (searchValue.Equals(bmp.GetPixel(x, y).ToArgb()))
              result.Add(new Point(x, y));
          }
        }
      }
      return result.ToArray();
    }

  }
}

How would I move my cursor to those coordinants then afterwards?

I tried this . . .

Cursor.Position = new System.Drawing.Point(points);

But got this error . . .

Argument '1': cannot convert from 'System.Drawing.Point[]' to 'System.Drawing.Size' (CS1503)

Any ideas?

The problem is you're more than like going to get more than one pixel that contains that color.... Like the windows task bar is the same color grey so if you match it then you will match all of the bottom pixels on your display. Anyway, to move your cursor to the first located pixel.

Modify the button click:

private void button1_Click(object sender, EventArgs e)
    {
      Point[] points = FindColor(button1.BackColor);
      if (points.Length > 0)
        Cursor.Position = points[0];
    }

Is there a faster method of doing the same job as this?

When I try it really quickly, the program eats all my CPU and lags until finished.

Why don't you tell me what you're really trying to do? Yes, there is a faster way. The way i'm doing it processes the *entire* image but it could be changed to stop after it matched the first pixel.

Locating screen pixels is not very accurate and i'm having a hard time envisioning what useful task you could be doing with this :)
Please elaborate

Ok, I'm trying to get my program to search for a selected color on the screen, and then move your cursor to the x,y position of that color afterwards.

This is called a 'Color Aim Bot' or a 'Pixel Chaser'.

It needs to updated very quickly (around 25 - 50ms) and act very quickly aswell to be effective.

See where I'm going with this? :)

Nevermind, found some great code and its exactly what I've been looking for.

Thank you for your help!!

SOLVED

Please share :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.