Hey, I found a cool class online for finding certain colors in a designated rectangle around your mouse position.

Its optimised for speed, and supposedly works, but when I try compiling it I get around 32 errors.

Heres the code

public static Point PixelSearch(Rectangle rect, int PixelColor, int Shade_Variation)
        {
            Color Pixel_Color = Color.FromArgb(PixelColor);

            Point Pixel_Coords = new Point(-1, -1);
            Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect);
            BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr

            unsafe
            {
                for (int y = 0; y < RegionIn_BitmapData.Height; y++)
                {
                    byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);

                    for (int x = 0; x < RegionIn_BitmapData.Width; x++)
                    {
                        if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
                        {
                            if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
                            {
                                if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
                                {
                                    Pixel_Coords = new Point(x + rect.X, y + rect.Y);
                                    goto end;
                                }
                            }
                        }
                    }
                }
            }

        end:
            return Pixel_Coords;
        }

        private static Bitmap CaptureScreenRegion(Rectangle rect)
        {
            Bitmap BMP = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
            Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
            GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
            return BMP;
        }

I found it here > http://www.elitepvpers.de/forum/gamehacking-coding/247732-c-pixelsearch-search-screen-pixel.html

Unsafe code allowing is enabled.
Im using SharpDevelop as my developer.

Recommended Answers

All 5 Replies

I compiled it fine after I set up to allow unsafe:

using System.Drawing;
using System.Drawing.Imaging;

namespace daniweb
{
  class PixelSearcher
  {

    public static Point PixelSearch(Rectangle rect, int PixelColor, int Shade_Variation)
    {
      Color Pixel_Color = Color.FromArgb(PixelColor);

      Point Pixel_Coords = new Point(-1, -1);
      Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect);
      BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

      int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr

      unsafe
      {
        for (int y = 0; y < RegionIn_BitmapData.Height; y++)
        {
          byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);

          for (int x = 0; x < RegionIn_BitmapData.Width; x++)
          {
            if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
            {
              if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
              {
                if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
                {
                  Pixel_Coords = new Point(x + rect.X, y + rect.Y);
                  goto end;
                }
              }
            }
          }
        }
      }

    end:
      return Pixel_Coords;
    }

    private static Bitmap CaptureScreenRegion(Rectangle rect)
    {
      Bitmap BMP = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
      Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
      GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
      return BMP;
    }
  }
}
commented: Always helping me out. Thank you! +1

I love how when I put your code in my class, it works fine, but mine doesn't.

I think I was missing the namespace . . . Haha.

Thank you!

commented: heh +10

Sorry, but how would I go about moving my cursor to a searched position . . . I don't really understand the code :\


Thank you

I didn't change anything, same class:

using System.Drawing;
using System.Drawing.Imaging;

namespace daniweb
{
  class PixelSearcher
  {

    public static Point PixelSearch(Rectangle rect, int PixelColor, int Shade_Variation)
    {
      Color Pixel_Color = Color.FromArgb(PixelColor);

      Point Pixel_Coords = new Point(-1, -1);
      Bitmap RegionIn_Bitmap = CaptureScreenRegion(rect);
      BitmapData RegionIn_BitmapData = RegionIn_Bitmap.LockBits(new Rectangle(0, 0, RegionIn_Bitmap.Width, RegionIn_Bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

      int[] Formatted_Color = new int[3] { Pixel_Color.B, Pixel_Color.G, Pixel_Color.R }; //bgr

      unsafe
      {
        for (int y = 0; y < RegionIn_BitmapData.Height; y++)
        {
          byte* row = (byte*)RegionIn_BitmapData.Scan0 + (y * RegionIn_BitmapData.Stride);

          for (int x = 0; x < RegionIn_BitmapData.Width; x++)
          {
            if (row[x * 3] >= (Formatted_Color[0] - Shade_Variation) & row[x * 3] <= (Formatted_Color[0] + Shade_Variation)) //blue
            {
              if (row[(x * 3) + 1] >= (Formatted_Color[1] - Shade_Variation) & row[(x * 3) + 1] <= (Formatted_Color[1] + Shade_Variation)) //green
              {
                if (row[(x * 3) + 2] >= (Formatted_Color[2] - Shade_Variation) & row[(x * 3) + 2] <= (Formatted_Color[2] + Shade_Variation)) //red
                {
                  Pixel_Coords = new Point(x + rect.X, y + rect.Y);
                  goto end;
                }
              }
            }
          }
        }
      }

    end:
      return Pixel_Coords;
    }

    private static Bitmap CaptureScreenRegion(Rectangle rect)
    {
      Bitmap BMP = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
      Graphics GFX = System.Drawing.Graphics.FromImage(BMP);
      GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
      return BMP;
    }
  }
}

Calling it:

private void button4_Click(object sender, EventArgs e)
    {
      button4.BackColor = Color.Red;
      //You have to invalidate and repaint or else the button wont change colors until
      //AFTER the screenshot happens, thus you wont find the pixel!
      this.Invalidate(true);
      this.Update();
      Color searchColor = button4.BackColor;
      Rectangle clip = new Rectangle(this.Location, this.Size); //only search the active window
      Point loc = PixelSearcher.PixelSearch(clip, searchColor.ToArgb(), 5);
      Cursor.Position = loc;
    }

Please mark this thread as solved if you have found an answer to your original quesiton and good luck!

Works perfectly. You're a genius! +REP & Solved.

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.