特定の色を持つ最初のピクセルをクリックするプログラムを作成しようとしています。残念ながら、画面上に実際に色があることをプログラムが検出できないことがあるようです。画面のスクリーンショットを撮り、GetPixel() メソッドを使用してすべてのピクセルの色を見つけます。
これが私が使用する私の方法です:
private static Point FindFirstColor(Color color)
{
int searchValue = color.ToArgb();
Point location = Point.Empty;
using (Bitmap bmp = GetScreenShot())
{
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
if (searchValue.Equals(bmp.GetPixel(x, y).ToArgb()))
{
location = new Point(x, y);
}
}
}
}
return location;
}
画面のスクリーンショットを撮るために、次を使用します。
private static Bitmap GetScreenShot()
{
Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
{
using (Graphics gfx = Graphics.FromImage(result))
{
gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
}
}
return result;
}
画面上にあることがわかっている色を使用しても、Point.Empty が返されます。これの理由は何ですか?