我想检查一个像素,在某个区域,是否有特定的颜色。目前我只能查看屏幕的中间部分。但我意识到,我宁愿从屏幕中央扫描一个10x10的盒子。
这是我现在实际使用的代码。
Point xy = new Point(Screen.PrimaryScreen.Bounds.Width / 2 + 1, Screen.PrimaryScreen.Bounds.Height / 2 + 1);
Color GetPixel(Point position)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
}
return bitmap.GetPixel(0, 0);
}
}
color = GetPixel(xy);
Color purple = Color.FromArgb(255,254,93,255);
if (color.Equals(purple) == true).....
是否有从10x10扫描紫色框的选项,当颜色在此框中时返回true ?
尝试使用这个函数:
private bool ContainsColor(Point StartPosition, int BoxSize, Color ColorToScanFor)
{
using (Bitmap image = new Bitmap(BoxSize, BoxSize))
{
using (Graphics graphics = Graphics.FromImage(image))
{
graphics.CopyFromScreen(StartPosition, Point.Empty, new Size(BoxSize, BoxSize));
for (int X = 0; X < image.Width; X++)
{
for (int Y = 0; Y < image.Height; Y++)
{
if (image.GetPixel(X, Y).ToArgb() == ColorToScanFor.ToArgb())
{
return true;
}
}
}
}
}
return false;
}
当你想调用这个函数时,试着这样做:
MessageBox.Show(ContainsColor(new Point(0, 0), 10, Color.Black).ToString());