C# 以编程方式从笔刷类获取笔刷?

C# 以编程方式从笔刷类获取笔刷?,c#,brush,brushes,C#,Brush,Brushes,我有一个属性,允许将已知颜色的字符串名称发送到我的控件。酒店只接受正确的已知颜色名称,如“红色”或“蓝色” 而不是像这样绘制控件时创建新笔刷 Brush sysBrush = Brushes.FromKnownColor(this._UseColor); e.Graphics.FillRectangle(sysBrush, 0, 0, 10, 10); using (SolidBrush brsh = new SolidBrush(Color.FromKnownColor(this._UseC

我有一个属性,允许将已知颜色的字符串名称发送到我的控件。酒店只接受正确的已知颜色名称,如“红色”或“蓝色”

而不是像这样绘制控件时创建新笔刷

Brush sysBrush = Brushes.FromKnownColor(this._UseColor);
e.Graphics.FillRectangle(sysBrush, 0, 0, 10, 10);
using (SolidBrush brsh = new SolidBrush(Color.FromKnownColor(this._UseColor)))
    e.Graphics.FillRectangle(brsh, 0, 0, 10, 10);
是否有人知道这是可能的,或者我每次都必须创建一个新的笔刷


画笔。FromKnownColor
不是
画笔
类中的一个方法,为什么不创建一次画笔并将其缓存以供以后使用

在你的主课上:

private KnownColor _UseColor = KnownColor.Red;

/// <summary>
/// Gets or sets the name of the colour
/// </summary>
public string ColorName
{
    get
    {
        return this._UseColor.ToString();
    }
    set
    {
        if (Enum.IsDefined(typeof(KnownColor), value))
            this._UseColour = (KnownColor)Enum.Parse(typeof(KnownColor), value);
    }
}

private Dictionary<string, Brush> _knownBrushes = new Dictionary<string, Brush>();

public Brush ColorBrush
{
    get
    {
        if (!_knownBrushes.ContainsKey(_UseColor)) {
            _knownBrushes[_UseColor] = new SolidBrush(Color.FromKnownColor(this._UseColor));
        }

        return _knownBrushes[_UseColor];
    }
}
反思方法
你的案子 字段:

PropertyInfo[] _properties = typeof (Brushes).GetProperties();
静态法 用法:

var knownBrush = GetKnownBrush(ColorName);
var knownBrush = KnownBrush;
实例属性 用法:

var knownBrush = GetKnownBrush(ColorName);
var knownBrush = KnownBrush;


您还可以将经常使用的画笔存储在字典中,以避免反射动作。

如果您想要通过颜色查找画笔的解决方案,即使颜色可能没有已知名称,也可以创建使用该颜色的字典:

void Main()
{
    var brush = KnownBrush(Color.FromArgb(255, 0, 0));
    brush.Dump();
}

private static Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush> _KnownBrushes;
public static SolidBrush KnownBrush(Color color)
{
    if (_KnownBrushes == null)
    {
        _KnownBrushes = new Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush>();
        foreach (var propertyInfo in typeof(Brushes).GetProperties())
        {
            if (propertyInfo.PropertyType == typeof(Brush))
            {
                var brush = propertyInfo.GetValue(null) as SolidBrush; // not a typo
                if (brush != null)
                    _KnownBrushes[Tuple.Create(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A)] = brush;
            }
        }
    }

    SolidBrush result;
    _KnownBrushes.TryGetValue(Tuple.Create(color.R, color.G, color.B, color.A), out result);
    return result;
}
void Main()
{
var brush=KnownBrush(Color.FromArgb(255,0,0));
brush.Dump();
}
私有静态字典_KnownBrushes;
公共静态SolidBrush KnownBrush(彩色)
{
如果(_KnownBrushes==null)
{
_KnownBrushes=新字典();
foreach(typeof中的var propertyInfo(刷子).GetProperties())
{
if(propertyInfo.PropertyType==typeof(画笔))
{
var brush=propertyInfo.GetValue(null)作为SolidBrush;//不是输入错误
如果(画笔!=null)
_KnownBrushes[Tuple.Create(brush.Color.R,brush.Color.G,brush.Color.B,brush.Color.A)]=画笔;
}
}
}
固刷结果;
_KnownBrushes.TryGetValue(Tuple.Create(color.R、color.G、color.B、color.A)、out结果);
返回结果;
}

其他答案很复杂。这里有一个将字符串“紫色”转换为纯色笔刷的单行线:

new SolidColorBrush((Color)ColorConverter.ConvertFromString("purple"))

记住
使用System.Windows.Media

我喜欢这样,谢谢:)缓存笔刷对象通常比动态创建它们好吗?那么,我不需要让我的类成为一次性的,以便在最后处理_knownbrush吗?你可以让它在类的析构函数中处理每个画笔。是的。在_knownbrush中可能不会有大量的画笔,但我仍然会选择调用TryGetValue(),而不是在getter中进行双重查找。
var knownBrush = KnownBrush;
void Main()
{
    var brush = KnownBrush(Color.FromArgb(255, 0, 0));
    brush.Dump();
}

private static Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush> _KnownBrushes;
public static SolidBrush KnownBrush(Color color)
{
    if (_KnownBrushes == null)
    {
        _KnownBrushes = new Dictionary<Tuple<byte, byte, byte, byte>, SolidBrush>();
        foreach (var propertyInfo in typeof(Brushes).GetProperties())
        {
            if (propertyInfo.PropertyType == typeof(Brush))
            {
                var brush = propertyInfo.GetValue(null) as SolidBrush; // not a typo
                if (brush != null)
                    _KnownBrushes[Tuple.Create(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A)] = brush;
            }
        }
    }

    SolidBrush result;
    _KnownBrushes.TryGetValue(Tuple.Create(color.R, color.G, color.B, color.A), out result);
    return result;
}
new SolidColorBrush((Color)ColorConverter.ConvertFromString("purple"))