The code below when invoked to get a pixel on multiple screens or one screen with Display zoom on it, e.g. 150%, it doesn't get the right pixel in 3840x2160 pixels, it gets another pixel because of the zoom, how to fix that plz?
What I have tried:
static Color GetPixel(Point p)
{
using (var bitmap = new Bitmap(1, 1))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen(p, new Point(0, 0), new Size(1, 1));
}
return bitmap.GetPixel(0, 0);
}
}
I do this:
if (formMain.WindowState != FormWindowState.Minimized)
{
if (
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50, formMain.pictureBoxCleanup.Top + 20))) == Color.Gray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 1, formMain.pictureBoxCleanup.Top + 20))) == Color.DarkGray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 2, formMain.pictureBoxCleanup.Top + 20))) == Color.Brown) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50, formMain.pictureBoxCleanup.Top + 21))) == Color.RosyBrown) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 1, formMain.pictureBoxCleanup.Top + 21))) == Color.DarkSlateGray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 2, formMain.pictureBoxCleanup.Top + 21))) == Color.DarkGray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50, formMain.pictureBoxCleanup.Top + 22))) == Color.SlateGray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 1, formMain.pictureBoxCleanup.Top + 22))) == Color.LightSlateGray) &&
(ClassGetPixelWithDisplayZoomInMind.get_pixel(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 2, formMain.pictureBoxCleanup.Top + 22))) == Color.LightGray))
{
Program.DoMouseClick(formMain.PointToScreen(new Point(formMain.pictureBoxCleanup.Right + 50 + 1, formMain.pictureBoxCleanup.Top + 21)));
}
}
And this:
private void FormMain_Paint(object sender, PaintEventArgs e)
{
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50, 20, Color.Gray);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 1,20, Color.DarkGray);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 2, 20, Color.Brown);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50, 21, Color.RosyBrown);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 1, 21, Color.White);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 2, 21, Color.DarkGray);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50, 22, Color.SlateGray);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 1, 22, Color.LightSlateGray);
ClassSetGetPixel.SetPixel(this, pictureBoxCleanup.Right + 50 + 2, 22, Color.LightGray);
}