简体   繁体   English

从颜色转换为画笔

[英]convert from Color to brush

How do I convert a Color to a Brush in C#? C# 中如何将Color转换为Brush

This is for Color to Brush ....这是用于Brush Color ....

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现在,如果您需要在 XAML 中使用它,您可以制作一个自定义值转换器并在绑定中使用它

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.重点是并非所有画笔都是颜色,但您可以将所有颜色转换为 (SolidColor)Brush。

SolidColorBrush brush = new SolidColorBrush( Color.FromArgb(255,255,139,0) )

you can use this:你可以使用这个:

new SolidBrush(color)

where color is something like this:颜色是这样的:

Color.Red

or或者

Color.FromArgb(36,97,121))

or ...或者 ...

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.如果您碰巧使用混合了 Windows 窗体和 WPF 的应用程序,您可能会遇到尝试将 System.Drawing.Color 转换为 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.然后您可以使用前面提到的技术之一转换为 Brush。

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);
        }
    }

}

Here is the Easiest way, No Helpers, No Converters, No weird Media references.这是最简单的方法,没有助手,没有转换器,没有奇怪的媒体参考。 Just 1 line:只有 1 行:

System.Drawing.Brush _Brush = new SolidBrush(System.Drawing.Color.FromArgb(this.ForeColor.R, this.ForeColor.G, this.ForeColor.B));

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.为此目的使用兄弟姐妹或父母的画笔通常就足够了,并且通过检索他们的 Foreground 或 Background 属性在 wpf 中很容易获得。

ref: Control.Background参考: Control.Background

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM