System.Drawing.Color to System.Windows.Media.Brush
-
Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.
System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); if (\_color.IsNamedColor) { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name); } else { ??????????? }
What should I put at the ?????????
-
Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.
System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); if (\_color.IsNamedColor) { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name); } else { ??????????? }
What should I put at the ?????????
You can use Color.FromArgb to generate the appropriate values.
ColorBrush.Color.FromArgb(_color.ToArgb());
I'm doing this off the top of my head, so you may need to tweak it slightly but this should work.
Deja View - the feeling that you've seen this post before.
-
You can use Color.FromArgb to generate the appropriate values.
ColorBrush.Color.FromArgb(_color.ToArgb());
I'm doing this off the top of my head, so you may need to tweak it slightly but this should work.
Deja View - the feeling that you've seen this post before.
-
Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.
System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); if (\_color.IsNamedColor) { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name); } else { ??????????? }
What should I put at the ?????????
I Found It.
System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); if (\_color.IsNamedColor) { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name); } else { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString("#"+\_color.Name); }
When it is not a NamedColor it returns for example ffffff80. When you add a # it recognizes and converts it. Thanks Anyway
-
The System.Windows.Media.Brush ColorBrush doesn't contain anything like Color or FromArgb. Neither does the BrushConverter.
Colours and Brushes are two different things. As mentioned above you can access the components of the colour individually to swap between a System.Drawing and a WPF color.
using (System.Windows.Forms.ColorDialog d = new System.Windows.Forms.ColorDialog())
{
d.ShowDialog();
System.Drawing.Color c = d.Color;
Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(c.A,c.R,c.G,c.B));
} -
Hi I'm creating a Windows Forms Application with an WPF Element Host. I want to the user to set the background of an WPF element with use of a ColorDialog. So I need to convert the System.Drawing.Color to a System.Windows.Media.Brush. I got it to work when the Color is a NamedColor (isNamedColor) but else I cannot get it to work.
System.Windows.Media.BrushConverter bc = new System.Windows.Media.BrushConverter(); if (\_color.IsNamedColor) { ColorBrush = (System.Windows.Media.Brush)bc.ConvertFromString(\_color.Name); } else { ??????????? }
What should I put at the ?????????