画刷和画刷类型
1.SolidBrush(单色画刷)
它是一种一般的画刷,通常只用一种颜色去填充GDI+图形,例如:
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush sdBrush1 = new SolidBrush(Color.Red); SolidBrush sdBrush2 = new SolidBrush(Color.Green); SolidBrush sdBrush3 = new SolidBrush(Color.Blue); g.FillEllipse(sdBrush2, 20, 40, 60, 70); Rectangle rect = new Rectangle(0, 0, 200, 100); g.FillPie(sdBrush3, 0, 0, 60, 60, 0, 270); PointF point1 = new PointF(50.0f, 250.0F); PointF point2 = new PointF(100.0f, 25.0F); PointF point3 = new PointF(150.0f, 40.0F); PointF point4 = new PointF(250.0f, 50.0F); PointF point5 = new PointF(300.0f, 100.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5 }; g.FillPolygon(sdBrush1, curvePoints); }
2.HatchBrush(阴影画刷)
HatchBrush类有两个构造函数:
l public HatchBrush(HatchStyle,Color forecolor);
l public HatchBrush(HatchStyle,Color forecolor,Color backcolor);
Graphics g = e.Graphics; HatchBrush hBrush1 = new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red); HatchBrush hBrush2 = new HatchBrush(HatchStyle.DashedHorizontal, Color.Green, Color.Black); HatchBrush hBrush3 = new HatchBrush(HatchStyle.Cross, Color.BlueViolet, Color.Blue); g.FillEllipse(hBrush1, 20, 80, 60, 20); g.FillPie(hBrush3, 0, 0, 200, 40, 0.0f, 30.0f); Rectangle rect = new Rectangle(0, 0, 200, 100); PointF point1 = new PointF(50.0f, 250.0F); PointF point2 = new PointF(100.0f, 25.0F); PointF point3 = new PointF(150.0f, 40.0F); PointF point4 = new PointF(250.0f, 50.0F); PointF point5 = new PointF(300.0f, 100.0F); PointF[] curvePoints = { point1, point2, point3, point4, point5 }; g.FillPolygon(hBrush2, curvePoints);
3.TextureBrush(纹理画刷)
Graphics g = e.Graphics; //根据文件名创建原始大小的bitmap对象 Bitmap bitmap = new Bitmap("D:\\mm.jpg"); //将其缩放到当前窗体大小 bitmap = new Bitmap(bitmap, this.ClientRectangle.Size); TextureBrush myBrush = new TextureBrush(bitmap); g.FillEllipse(myBrush, this.ClientRectangle);
4.LinearGradientBrush和PathGradientBrush(渐变画刷)
渐变画刷有两种:线性画刷和路径画刷(LinearGradientBrush和PathGradientBrush)。
其中LinearGradientBrush可以显示线性渐变效果,而PathGradientBrush是路径渐变的可以显示比较具有弹性的渐变效果。
(1)LinearGradientBrush类
LinearGradientBrush类构造函数如下:
public LinearGradientBrush(Point point1,Point point2,Color color1,Color color2)
参数说明:
point1:表示线性渐变起始点的Point结构。
point2:表示线性渐变终结点的Point结构。
color1:表示线性渐变起始色的Color结构。
color2:表示线性渐变结束色的Color结构。
Graphics g = e.Graphics; LinearGradientBrush myBrush = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Blue, LinearGradientMode.Vertical); g.FillRectangle(myBrush, this.ClientRectangle);
(2)PathGradientBrush类
PathGradientBrush类的构造函数如下:
public PathGradientBrush (GraphicsPath path);
参数说明:
path:GraphicsPath,定义此PathGradientBrush填充的区域。
例子代码如下:
Graphics g = e.Graphics; Point centerPoint = new Point(150, 100); int R = 60; GraphicsPath path = new GraphicsPath(); path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R); PathGradientBrush brush = new PathGradientBrush(path); //指定路径中心点 brush.CenterPoint = centerPoint; //指定路径中心的颜色 brush.CenterColor = Color.Red; //Color类型的数组指定与路径上每个顶点的颜色 brush.SurroundColors = new Color[] { Color.Plum }; g.FillEllipse(brush, centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R); centerPoint = new Point(350, 100); R = 20; path = new GraphicsPath(); path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R); path.AddEllipse(centerPoint.X - 2 * R, centerPoint.Y - 2 * R, 4 * R, 4 * R); path.AddEllipse(centerPoint.X - 3 * R, centerPoint.Y - 3 * R, 6 * R, 6 * R); brush = new PathGradientBrush(path); brush.CenterPoint = centerPoint; brush.CenterColor = Color.Red; brush.SurroundColors = new Color[] { Color.Black, Color.Blue, Color.Green }; g.FillPath(brush, path);