【问题标题】:Get a Color from a position in my form in C#从 C# 表单中的某个位置获取颜色
【发布时间】:2020-01-23 09:22:47
【问题描述】:

如何才能读取应用程序中某个位置的颜色代码? (红圈)

然后我可以输出richtextbox中的值?

感谢您的帮助。

【问题讨论】:

标签: c# colors


【解决方案1】:

您可以使用 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()
  • 好的,我已经对此进行了测试,我收到此错误消息“该名称在上下文中不可用”
  • 检查你的 textBox 名称,可能真的像 textBox1
  • 我添加了以下代码:textBox1.Text = GetColorAt(x, y).ToString();,但我仍然收到错误消息
猜你喜欢
  • 2011-11-16
  • 2014-10-14
  • 1970-01-01
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多