屏幕区域搜索颜色

时间:2013-08-07 06:10:25

标签: c# screen pixel point

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

我能够找到屏幕上的所有像素并测试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);

我有时会从中获得数千个结果,并且只会喜欢一些。任何想法我怎么能这样做?

0 个答案:

没有答案