【问题标题】:Geting a inverted result from using FromArgb R = B? where does this fail?使用 FromArgb R = B 得到反转结果?这在哪里失败?
【发布时间】:2009-11-25 17:03:11
【问题描述】:

使用此逻辑时,R 和 B 出错,我似乎找不到我做错了什么,我的解决方法是我翻转 r 和 b 一点也不好,我试图找到逻辑中断的地方.

label1.Text = colorX;显示 R=255, G=0, B=0 当它应该是 R=0, G=0, B=255,这在哪里失败?

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;

    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);
    }
 }
}

【问题讨论】:

    标签: c# pixel argb


    【解决方案1】:

    我认为你以一种奇怪的方式解决这个问题;颜色具有 R、G 和 B 属性,您可以使用它来代替字符串匹配:

    R = colorX.R;
    G = colorX.G;
    B = colorX.B;
    

    其次,为了解决您的问题,GetPixel 可能正在获取 BGR 格式的像素而不是 RGB。 BGR 常用于位图中,您正在与之交谈的窗口的 HDC 很可能以这种格式返回。

    编辑:来自MSDN 文档:

    当指定明确的 RGB 颜色时, COLORREF 值具有以下内容 十六进制形式。 0x00bbggrr

    Color.FromArgb() 方法需要 0xAARRGGBB。您的解决方法很好,尽管正确的解决方法是在调用 .FromArgb() 方法之前翻转 R 和 B 组件:

    int color = GetPixel(...);
    return Color.FromArgb(color & 0xFF, color >> 8 & 0xFF, color >> 16);
    

    【讨论】:

    • 感谢您抽出宝贵时间查看此内容,好的 awnser,我的代码已按照您的建议修复:)
    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多