Color on screen area search
I'm trying to find patches of a certain color on the screen.
I am able to find all the pixels on the screen and test the RGB and add
the points into an array, but i don't want all the pixels just 1 point for
each patch of the color.
Here is my code:
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));
}
I call the method like this:
points = PixelSearch(Screen.PrimaryScreen.Bounds, Color.FromArgb(255, 0,
0), 15);
I sometimes get thousands of results from this and would only like a few.
any ideas how i can do this?
No comments:
Post a Comment