[英]What is the proper way to create a brush from a color?
What is the proper way to create a brush from a color? 用颜色创建画笔的正确方法是什么? I've tried
Brush b = new Brush(color);
我已经尝试了
Brush b = new Brush(color);
but Brush
but that isn't allowed. 但是
Brush
但是那是不允许的。 I can get existing colors using Brushes
, but that doesn't have a way to create a specific brush. 我可以使用
Brushes
获得现有的颜色,但是没有创建特定画笔的方法。 I'm using my to fill a rectangle with solid color. 我正在用单色填充矩形。
The current code I have involves creating a Pen
and then taking its Brush
, but that doesn't seem like the right way: 我当前使用的代码涉及创建
Pen
,然后使用它的Brush
,但这似乎不是正确的方法:
Brush b = new Pen(color).Brush;
What is the correct way I should go about doing this? 我应该这样做的正确方法是什么?
The two current answers cover the two likely possibilities; 当前的两个答案涵盖了两种可能的可能性: if you're using GDI+ you want
new SolidBrush(color)
and if you're using WPF you want new SolidColorBrush(color)
. 如果使用的是GDI +,则需要
new SolidBrush(color)
;如果使用的是WPF,则需要new SolidColorBrush(color)
。
The reason is that with either, Brush
is an abstract class that covers a range of possible brushes that could tile a bitmap or apply a gradient or otherwise paint with something other than just a single colour. 原因是,无论使用哪种
Brush
, Brush
都是一个抽象类,它涵盖了可能的画笔范围,这些画笔可以平铺位图,应用渐变或以其他方式用单色以外的颜色进行绘制。
您需要创建SolidColorBrush :
Brush b = new SolidColorBrush(color);
尝试为GDI +创建新的SolidBrush
:
SolidBrush mySolidBrush = new SolidBrush(color);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.