0

我试图在屏幕上找到某种颜色的补丁。

我能够找到屏幕上的所有像素并测试 RGB 并将这些点添加到一个数组中,但我不希望所有像素对于每个颜色补丁只有 1 个点。

这是我的代码:

    public static Point[] PixelSearch(Rectangle rect, Color Pixel_Color, int Shade_Variation)
    {
        ArrayList points = new ArrayList();
        Bitmap RegionIn_Bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
        using (Graphics GFX = Graphics.FromImage(RegionIn_Bitmap))
        {
            GFX.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);
        }
        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
                                points.Add(new Point(x + rect.X, y + rect.Y));
            }
        }
        RegionIn_Bitmap.Dispose();
        return (Point[])points.ToArray(typeof(Point));
    }

我这样调用方法:

points = PixelSearch(Screen.PrimaryScreen.Bounds, Color.FromArgb(255, 0, 0), 15);

我有时会从中得到数千个结果,并且只想要几个。任何想法我怎么能做到这一点?

4

0 回答 0