/// <summary> /// Get the color from the pixel on the screen at "x,y" /// </summary> /// <param name="screenCoordinates">Point with the coordinates</param> /// <returns>Color at the specified screenCoordinates</returns> private static Color GetPixelColor(Point screenCoordinates) { using (SafeWindowDcHandle screenDC = SafeWindowDcHandle.FromDesktop()) { try { uint pixel = GDI32.GetPixel(screenDC, screenCoordinates.X, screenCoordinates.Y); Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16); return(color); } catch (Exception) { return(Color.Empty); } } }
/// <summary> /// Get the color from the pixel on the screen at "x,y" /// </summary> /// <param name="screenCoordinates">Point with the coordinates</param> /// <returns>Color at the specified screenCoordinates</returns> static private Color GetPixelColor(Point screenCoordinates) { IntPtr hdc = User32.GetDC(IntPtr.Zero); try { uint pixel = GDI32.GetPixel(hdc, screenCoordinates.X, screenCoordinates.Y); Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16); return(color); } catch (Exception) { return(Color.Empty); } finally { if (hdc != IntPtr.Zero) { User32.ReleaseDC(IntPtr.Zero, hdc); } } }