C# 从我的表单中的一个位置在C中获取颜色#

C# 从我的表单中的一个位置在C中获取颜色#,c#,colors,C#,Colors,我怎么可能在应用程序中的某个位置读取颜色代码?(红色圆圈) 然后我可以在richtextbox中输出值 感谢您的帮助。您可以使用GetPixel函数获取像素的颜色: [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", SetLastError = true)] public static extern Int

我怎么可能在应用程序中的某个位置读取颜色代码?(红色圆圈)

然后我可以在
richtextbox
中输出值


感谢您的帮助。

您可以使用GetPixel函数获取像素的颜色:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr window);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr dc, int x, int y);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr window, IntPtr dc);

public static Color GetColorAt(int x,  int y)
{
    IntPtr desk = GetDesktopWindow();
    IntPtr dc = GetWindowDC(desk);
    int a = (int) GetPixel(dc, x, y);
    ReleaseDC(desk, dc);
    return Color.FromArgb(255, (a >> 0) & 0xff, (a >> 8) & 0xff, (a >> 16) & 0xff);
}

尝试一下如何将此值添加到我的文本框中?Textbox.Text=GetColorAt(x,y).ToString()好的,我已经进行了测试,我收到了一条错误消息“该名称在上下文中不可用”检查您的文本框名称,可能与textBox1类似,实际上我已经添加了以下代码:textBox1.Text=GetColorAt(x,y).ToString()我仍然收到错误消息