有没有办法从WPF中的ColorDialog获取RGB值? [英] Is there any way to get RGB value from ColorDialog in WPF?

查看:121
本文介绍了有没有办法从WPF中的ColorDialog获取RGB值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



有没有办法从WPF中的ColorDialog获取RGB值?



我试过以下代码但是在这里我收到错误



错误4无法隐式转换类型'System.Drawing .Color'到'System.Windows.Media.Color'





代码:

 System.Windows.Forms.ColorDialog cd =  new  System.Windows.Forms.ColorDialog(); 
颜色clr = cd.Color;







谢谢

解决方案

Color类具有属性

  public   byte  R { get ; } 
public byte G { get ; }
public byte B { get ; }





此片段显示了如何获取RGB值:

 ColorDialog cd =  new  ColorDialog(); 

DialogResult result = cd.ShowDialog();

if (result == DialogResult.OK)
{
Color color = cd.Color;

byte R = color.R;
byte G = color.G;
byte B = color.B;
}


无法将类型'System.Drawing.Color'隐式转换为'System.Windows.Media。颜色'



问题是你使用的是Windows Forms ColorDialog ,它返回一个系统.Drawing.Color ,而WPF倾向于使用 System.Windows.Media.Color



您可以切换到使用WPF颜色选择器 - 例如,这一个 [ ^ ]。



或者,您需要转换返回的颜色:

  var  cd =  new  System.Windows.Forms.ColorDialog(); 
...
var winFormsColor = cd.Color;
var wpfColor = System.Windows.Media.Color.FromArgb(winFormsColor.A,winFormsColor.R,winFormsColor.G,winFormsColor.B);


Hi Friends,

Is there any way to get the RGB value from ColorDialog in WPF?

I have tried following code but but here i'm getting error as

Error	4	Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Color'



Code :

System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
 Color clr = cd.Color;




Thanks

解决方案

The Color class has properties

public byte R { get; }
public byte G { get; }
public byte B { get; }



This snippet shows how to get the RGB values:

ColorDialog cd = new ColorDialog();

DialogResult result = cd.ShowDialog();

if (result == DialogResult.OK)
{
    Color color = cd.Color;

    byte R = color.R;
    byte G = color.G;
    byte B = color.B;
}


Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Color'


The problem is that you're using the Windows Forms ColorDialog, which returns a System.Drawing.Color, whereas WPF tends to use System.Windows.Media.Color.

You could switch to using a WPF colour picker - for example, this one[^].

Alternatively, you'll need to convert the returned colour:

var cd = new System.Windows.Forms.ColorDialog();
...
var winFormsColor = cd.Color;
var wpfColor = System.Windows.Media.Color.FromArgb(winFormsColor.A, winFormsColor.R, winFormsColor.G, winFormsColor.B);


这篇关于有没有办法从WPF中的ColorDialog获取RGB值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆