我正在努力解决一些WPF问题。这只是其中之一:
{backgroundColor.SelectedColor = Properties.Settings.Default.backgroundColorSt;}
当我把它写入我的initiliaze()部分时,我遇到了如下错误:
无法隐式转换类型系统.drawing.color'至 ' system.windows.media.color'
如何将System.Drawing.Color转换为System.Windows:Media.Color以保存我从colorPicker中选择的颜色?
答案 0 :(得分:1)
首先,不要在System.Drawing.Color
设置中使用backgroundColorSt
类型。相反,请使用System.Windows.Media.Color
。
在项目属性的Settings
页面中,
backgroundColorSt
行Type
ComboBox,Browse...
,PresentationCore
程序集System.Windows.Media
,Color
。答案 1 :(得分:0)
要在它们之间进行转换,您可以这样做:
System.Drawing.Color drawingColor = Color.Red;
System.Windows.Media.Color mediaColor = System.Windows.Media.Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B);
针对您的具体问题,我建议将其存储为字符串:
//Save the Color from Colorpicker as HEX value to Settings:
System.Drawing.Color color = backgroundColor.SelectedColor;
//Assuming you store to string:
Properties.Settings.Default.backgroundColorSt = "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
//Later when reading the value from settings simply do this:
System.Windows.Media.ColorConverter.ConvertFromString(Properties.Settings.Default.backgroundColorSt);