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