画刷和画刷类型Brush类型是一个抽象类,所以它不能被实例化,也就是不能直接应用,但是我们可以利用它的派生类,如:HatchBrush、SolidBrush、TextureBrush、LinearGradientBrush和PathGradientBrush等。画刷类型一般在System.Drawing命名空间中,如果应用HatchBrush和GradientBrush画刷,需要在程序中引入System.Drawing.Drawing2D命名空间。
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); g.FillEllipse(sdBrush2, 20, 40, 60, 70); Rectangle rect = new Rectangle(0, 0, 200, 100); g.FillPie(sdBrush3, 0, 0, 200, 40, 0.0f, 30.0f ); PointF point1 = new PointF(50.0f, 250.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类位于System.Drawing.Drawing2D命名空间中。阴影画刷有两种颜色:前景色和背景色,以及6种阴影。前景色定义线条的颜色,背景色定各线条之间间隙的颜色。HatchBrush类有两个构造函数: public HatchBrush(HatchStyle,Color forecolor); public HatchBrush(HatchStyle,Color forecolor,Color backcolor); HatchStyle枚举值指定可用于HatchBrush对象的不同图案。 HatchStyle的主要成员如下表所示。 HatchStyle主要成员
下面代码显示了HatchBrush画刷的使用。 protected override void OnPaint(PaintEventArgs e) { 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.Weave, Color.BlueViolet, Color.Blue); g.FillEllipse(hBrush1, 20, 80, 60, 20); Rectangle rect = new Rectangle(0, 0, 200, 100); g.FillPie(hBrush3, 0, 0, 200, 40, 0.0f, 30.0f ); 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(纹理画刷) 纹理画刷拥有图案,并且通常使用它来填充封闭的图形。为了对它初始化,可以使用一个已经存在的别人设计好了的图案,或使用常用的设计程序设计的自己的图案,同时应该使图案存储为常用图形文件格式,如BMP格式文件。这里有一个设计好的位图,被存储为Papers.bmp文件。 private void Form1_Paint(object sender, PaintEventArgs e) { 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结构。 代码如下: private void Form1_Paint(object sender, PaintEventArgs e) { 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填充的区域。 例子代码如下: private void Form1_Paint(object sender, PaintEventArgs e) { 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); } |
|