WPF绘图中,经常回用到笔刷,下边总结一下怎么动态创建笔刷(在.cs文件中)
笔刷Brush的派生类比较多“
单色笔刷:SolidColorBrush
线性渐变笔刷:LinearGradientBrush
径向渐变笔刷:RadialGradientBrush
位图画刷:ImageBrush
矢量图画刷:DrawingBrush
可视元素画刷:VisualBrush
暂时先简单介绍一下单色画刷与线性渐变画刷
单色笔刷:SolidColorBrush
Label lineName = new Label();
lineName.Content = jxLine.sLineName;
SolidColorBrush sBrush = new SolidColorBrush();//单色画刷
lineName.Foreground = sBrush;
sBrush.Color = Colors.White ;
线性渐变笔刷LinearGradientBrush:
Rectangle rec = new Rectangle();
rec.Width = 100;
rec.Height = 300;
LinearGradientBrush lBrush = new LinearGradientBrush();//线性渐变画刷
lBrush.StartPoint = new Point(0, 0);
lBrush.EndPoint = new Point(1, 0);
rec.Fill = lBrush;
GradientStopCollection stpColor = new GradientStopCollection();
GradientStop stopColor = new GradientStop();
stopColor.Color = Colors.Yellow;
stopColor.Offset = 0.33;
stpColor.Add(stopColor);
GradientStop stopColor2 = new GradientStop();
stopColor2.Color = Colors.Red;
stopColor2.Offset = 0.66;
stpColor.Add(stopColor2);
GradientStop stopColor3 = new GradientStop();
stopColor3.Color = Colors.Blue;
stopColor3.Offset = 1.0;
stpColor.Add(stopColor3);
lBrush.GradientStops = stpColor;
在这里需要注意:线性渐变笔刷对于对象的填充(背景、前景等),赋值必须在定义颜色集合GradientStopCollection之前,否则并不能实现颜色的渐变填充。
其他笔刷控件的使用也与之类似。