Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取窗体工作区内像素的RGB值_C#_Macros_Rgb_Pixel - Fatal编程技术网

C# 如何获取窗体工作区内像素的RGB值

C# 如何获取窗体工作区内像素的RGB值,c#,macros,rgb,pixel,C#,Macros,Rgb,Pixel,我正在创建一个宏,需要检查一些像素,以确保鼠标点击的位置正确。如果您正在查看位图的RGB值,您将如何检查以c#为单位的像素的RGB值 using (Bitmap bitmap = new Bitmap(img)) { Color color = bitmap.GetPixel(0, 0); MessageBox.Show(color.R.toString() + color.G.toString() + color.B.toString()); } 经过大量的测试,这就是我的结局 using

我正在创建一个宏,需要检查一些像素,以确保鼠标点击的位置正确。如果您正在查看位图的RGB值,您将如何检查以c#为单位的像素的RGB值

using (Bitmap bitmap = new Bitmap(img))
{
Color color = bitmap.GetPixel(0, 0);
MessageBox.Show(color.R.toString() + color.G.toString() + color.B.toString());
}

经过大量的测试,这就是我的结局

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;


namespace Color_tool
{
public partial class Form1 : Form
{
    Regex rgbInputR;
    Regex rgbInputG;
    Regex rgbInputB;

    Match R, G, B;
    int r;
    int g;
    int b;


    string colorX;

    [DllImport("gdi32")]
    private static extern int GetPixel(IntPtr hdc, int x, int y);
    [DllImport("User32")]
    private static extern IntPtr GetWindowDC(IntPtr hwnd);

    private static readonly IntPtr DesktopDC = GetWindowDC(IntPtr.Zero);

    public static System.Drawing.Color GetPixelAtCursor()
    {
        System.Drawing.Point p = Cursor.Position;
        return System.Drawing.Color.FromArgb(GetPixel(DesktopDC, p.X, p.Y));
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        button1.BackColor = Color.Black;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        colorX = GetPixelAtCursor().ToString();
        Color backX = GetPixelAtCursor();
        this.BackColor = Color.FromArgb(r,g,b);
        label1.Text = colorX;
        RGB_value();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == false)
            timer1.Enabled = true;
        else
            timer1.Enabled = false;
    }

    private void RGB_value()
    {
        rgbInputR = new Regex(@"(?<=R=)\d{0,3}");
        rgbInputG = new Regex(@"(?<=G=)\d{0,3}");
        rgbInputB = new Regex(@"(?<=B=)\d{0,3}");

        Match R, G, B;

        R = rgbInputR.Match(colorX);
        G = rgbInputG.Match(colorX);
        B = rgbInputB.Match(colorX);
        //had to flip the R and B ???
        b = int.Parse(R.Groups[0].Value);
        g = int.Parse(G.Groups[0].Value);
        r = int.Parse(B.Groups[0].Value);
    }
 }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.Text.RegularExpressions;
名称空间颜色工具
{
公共部分类Form1:Form
{
Regex-rgbInputR;
Regex rgbInputG;
Regex rgbInputB;
匹配R,G,B;
INTR;
int g;
int b;
字符串colorX;
[DllImport(“gdi32”)]
私有静态外部intgetPixel(IntPtr hdc、intx、inty);
[DllImport(“User32”)]
私有静态外部IntPtr GetWindowDC(IntPtr hwnd);
私有静态只读IntPtr DesktopDC=GetWindowDC(IntPtr.Zero);
public static System.Drawing.Color GetPixelAtCursor()
{
System.Drawing.Point p=光标位置;
返回系统.Drawing.Color.FromArgb(GetPixel(DesktopDC,p.X,p.Y));
}
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
button1.BackColor=颜色。黑色;
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
colorX=GetPixelAtCursor().ToString();
Color backX=GetPixelAtCursor();
this.BackColor=Color.FromArgb(r,g,b);
label1.Text=colorX;
RGB_值();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
如果(timer1.Enabled==false)
timer1.Enabled=true;
其他的
timer1.Enabled=false;
}
私有无效RGB_值()
{
rgbInputR=新的正则表达式(@“(?)?