0

I'm using windows Forms app in visual studio 2019. Never really used c# besides this but i'm trying to display the color of the pixel in the mouse coordinates. However the rgb always equals 0 for all.

public static Point GetMousePositionWindowsForms()
{
    System.Drawing.Point point = Control.MousePosition;
    return new Point(point.X, point.Y);
}

public static string Getcolor()
{
    Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Color getcolor = screen.GetPixel(GetMousePositionWindowsForms().X-1, GetMousePositionWindowsForms().Y-1);
    return Convert.ToString(getcolor);
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
TheDankDex
  • 11
  • 4

1 Answers1

0

You must capture the screen before getting color. Try this:

public static string Getcolor()
{
    Bitmap screen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Graphics captureGraphics = Graphics.FromImage(screen);
    captureGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
    Color getcolor = screen.GetPixel(GetMousePositionWindowsForms().X-1, GetMousePositionWindowsForms().Y-1);
    return Convert.ToString(getcolor);
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52