二 基本画刷
1 Color (在System.Window.Media下) 除R、G、B外还有一个属性A 控制颜色的“不透明”程度,0-255,越小越透明。
Color clr = Color.FromRgb(r,g,b) //A值是255
Color clr = Color.FromArgb(a,r,g,b)
sRGB:电脑显示器或数码相机上适用的颜色空间
scRGB:ScR、ScG、ScB为单精度的浮点数,可以小于0或大于1,以容纳超出显示器和sRGB数字范围的颜色。
2 Brush
OBject
DispatcherObject
Freezable
Animatable
Brush(抽象类)
GradientBrush
LinearGradientBrush
RadialGradientBrush
SolidColorBrush
TileBrush
DrawingBrush
ImageBrush
VisualBrush
Color clr=Color.FromRgb(0,255,255);
SolidColorBrush brush = new SolidColorBrush(clr);
Background = bush
利用Brushes所取得的SolidColorBrush对象处于冻结(frozen)状态,不能再被改变。将对象冻结可以提高效率,冻结的Freezable对象可以在不同的线程之间共享。
从SystemColors返回的画刷对象都是冻结的,程序可以动态改变下面的画刷
Brush brush = new SystemColorBursh(SystemColors.windowcolor);
但不能改变下面的画刷
Brush brush = SystemColors.windowBrush;
3 渐变画刷 LinearGradientBrush,需要两个点,两个颜色值
LinearGradientBrush brush = new LinearGradientBrush(Colors.Red,Colors.Blue,new Point(0,0),new Point(1,1)
Background = brush;

Brush示例
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace Petzold.AdjustTheGradient
{
class AdjustTheGradient: Window
{
LinearGradientBrush brush;
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new AdjustTheGradient());
}
public AdjustTheGradient()
{
Title = "Adjust the Gradient";
SizeChanged += WindowOnSizeChanged;
brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 0);
brush.MappingMode = BrushMappingMode.Absolute;
Background = brush;
}
void WindowOnSizeChanged(object sender, SizeChangedEventArgs args)
{
double width = ActualWidth
- 2 * SystemParameters.ResizeFrameVerticalBorderWidth;
double height = ActualHeight
- 2 * SystemParameters.ResizeFrameHorizontalBorderHeight
- SystemParameters.CaptionHeight;
Point ptCenter = new Point(width / 2, height / 2);
Vector vectDiag = new Vector(width, -height);
Vector vectPerp = new Vector(vectDiag.Y, -vectDiag.X);
vectPerp.Normalize();
vectPerp *= width * height / vectDiag.Length;
brush.StartPoint = ptCenter + vectPerp;
brush.EndPoint = ptCenter - vectPerp;
}
}
}
LinearGradientBrush 使用GradientBrush定义的GradientStops Property,渐变颜色可以有多个,典型的应用如:可以实现七彩虹。

七彩虹示例:
namespace Petzold.FollowTheRainbow
{
class FollowTheRainbow: Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new FollowTheRainbow());
}
public FollowTheRainbow()
{
Title = "Follow the Rainbow";
LinearGradientBrush brush = new LinearGradientBrush();
brush.StartPoint = new Point(0, 0);
brush.EndPoint = new Point(1, 0);
Background = brush;
// Rainbow mnemonic is the name Roy G. Biv.
brush.GradientStops.Add(new GradientStop(Colors.Red, 0));
brush.GradientStops.Add(new GradientStop(Colors.Orange, .17));
brush.GradientStops.Add(new GradientStop(Colors.Yellow, .33));
brush.GradientStops.Add(new GradientStop(Colors.Green, .5));
brush.GradientStops.Add(new GradientStop(Colors.Blue, .67));
brush.GradientStops.Add(new GradientStop(Colors.Indigo, .84));
brush.GradientStops.Add(new GradientStop(Colors.Violet, 1));
}
}
}
4 渐变画刷 RadialGradientBrush 椭圆画刷,有Center,RadiusX,RadiusY等属性
5 BorderBrush 注意是Window的Brush Property,在客户区绘制边框
BorderBrush = new GradientBrush(Colors.Red,Colors.Blue,new Point(1,0),new Point(1,1));