132

How do I convert a Color to a Brush in C#?

Servy
  • 202,030
  • 26
  • 332
  • 449
kartal
  • 17,436
  • 34
  • 100
  • 145

8 Answers8

170

This is for Color to Brush....

you can't convert it, you have to make a new brush....

SolidColorBrush brush = new SolidColorBrush( myColor );

now, if you need it in XAML, you COULD make a custom value converter and use that in a binding

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
  • 13
    For backward compatibility, it would be better to use `SolidBrush` (`System.Drawing`), which is available since .NET Framework 1.1, instead of `SolidColorBrush` (`System.Windows.Media`), available starting from .NET Framework 3.0. – BillyJoe Jan 31 '18 at 15:45
  • SolidColorBrush doesnt work in my C# winforms .NETFramework,Version=v4.7.2 app & Visual Studio's suggestion was installation of some NuGet packages. SolidBrush works. Thanx @BillyJoe – Art Hansen Nov 09 '22 at 07:29
40
Brush brush = new SolidColorBrush(color);

The other way around:

if (brush is SolidColorBrush colorBrush)
    Color color = colorBrush.Color;

Or something like that.

Point being not all brushes are colors but you could turn all colors into a (SolidColor)Brush.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Color to Brush! – raiserle Jan 04 '18 at 16:30
  • @raiserle: For your information, the question content used to be `I want to convert from Brush to Color in c#` while the title was the other way around. – H.B. Jan 04 '18 at 19:36
  • I had to use `new SolidBrush(ColorTranslator.FromHtml("#0026FF"));` because `SolidColorBrush` is not happy with a Color parameter, it wants a media.Color as parameter – GuidoG Jun 15 '22 at 07:14
  • @GuidoG You could also use split it into the individual hex values and use `Color.FromArgb(0x00, 0x26, 0xFF)`. – H.B. Jun 15 '22 at 08:06
32
SolidColorBrush brush = new SolidColorBrush( Color.FromArgb(255,255,139,0) )
TruthOf42
  • 2,017
  • 4
  • 23
  • 38
21

you can use this:

new SolidBrush(color)

where color is something like this:

Color.Red

or

Color.FromArgb(36,97,121))

or ...

Omid.Hanjani
  • 1,444
  • 2
  • 20
  • 29
  • 2
    For backward compatibility, this answer is better, because `SolidBrush` (`System.Drawing`) is available since .NET Framework 1.1, while `SolidColorBrush` (`System.Windows.Media`) is available starting from .NET Framework 3.0. – BillyJoe Jan 31 '18 at 15:47
2

If you happen to be working with a application which has a mix of Windows Forms and WPF you might have the additional complication of trying to convert a System.Drawing.Color to a System.Windows.Media.Color. I'm not sure if there is an easier way to do this, but I did it this way:

System.Drawing.Color MyColor = System.Drawing.Color.Red;
System.Windows.Media.Color = ConvertColorType(MyColor);

System.Windows.Media.Color ConvertColorType(System.Drawing.Color color)
{
  byte AVal = color.A;
  byte RVal = color.R;
  byte GVal = color.G;
  byte BVal = color.B;

  return System.Media.Color.FromArgb(AVal, RVal, GVal, BVal);
}

Then you can use one of the techniques mentioned previously to convert to a Brush.

Jerry Pile
  • 21
  • 2
2

I had same issue before, here is my class which solved color conversions Use it and enjoy :

Here U go, Use my Class to Multi Color Conversion

using System;
using System.Windows.Media;
using SDColor = System.Drawing.Color;
using SWMColor = System.Windows.Media.Color;
using SWMBrush = System.Windows.Media.Brush;

//Developed by امین امیری دربان
namespace APREndUser.CodeAssist
{
    public static class ColorHelper
    {
        public static SWMColor ToSWMColor(SDColor color) => SWMColor.FromArgb(color.A, color.R, color.G, color.B);
        public static SDColor ToSDColor(SWMColor color) => SDColor.FromArgb(color.A, color.R, color.G, color.B);
        public static SWMBrush ToSWMBrush(SDColor color) => (SolidColorBrush)(new BrushConverter().ConvertFrom(ToHexColor(color)));
        public static string ToHexColor(SDColor c) => "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
        public static string ToRGBColor(SDColor c) => "RGB(" + c.R.ToString() + "," + c.G.ToString() + "," + c.B.ToString() + ")";
        public static Tuple<SDColor, SDColor> GetColorFromRYGGradient(double percentage)
        {
            var red = (percentage > 50 ? 1 - 2 * (percentage - 50) / 100.0 : 1.0) * 255;
            var green = (percentage > 50 ? 1.0 : 2 * percentage / 100.0) * 255;
            var blue = 0.0;
            SDColor result1 = SDColor.FromArgb((int)red, (int)green, (int)blue);
            SDColor result2 = SDColor.FromArgb((int)green, (int)red, (int)blue);
            return new Tuple<SDColor, SDColor>(result1, result2);
        }
    }

}
Amin AmiriDarban
  • 2,031
  • 4
  • 24
  • 32
2

Here is the Easiest way, No Helpers, No Converters, No weird Media references. Just 1 line:

System.Drawing.Brush _Brush = new SolidBrush(System.Drawing.Color.FromArgb(this.ForeColor.R, this.ForeColor.G, this.ForeColor.B));
Jhollman
  • 2,093
  • 24
  • 19
0

It's often sufficient to use sibling's or parent's brush for the purpose, and that's easily available in wpf via retrieving their Foreground or Background property.

ref: Control.Background