有没有办法从WPF中的ColorDialog获取RGB值? [英] Is there any way to get RGB value from ColorDialog in WPF?
问题描述
嗨朋友们,
有没有办法从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 FormsColorDialog
,它返回一个系统.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 FormsColorDialog
, which returns aSystem.Drawing.Color
, whereas WPF tends to useSystem.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屋!