画刷是用来填充区域的,在GDI+ 中,画刷分为五种: 单色画刷,也叫固体画刷(SolidBrush), 影线画刷(HatchBrush),纹理画刷(TextureBrush),线型渐变画刷(LinearGradientBrush),路径渐变画刷(PathGradientBrush)。这五种画刷全部都是由其共同的基类 Brush 类派生而来的,与画笔笔筒,画刷是用来填充区域的,所以画刷是没有宽度和长度的。五种画刷的简单介绍如下表:
画刷类别 | 定义类 | 画刷的基本描述 |
单色画刷 | SolidBrush | 用纯色填充图形 |
影线画刷 | HatchBrush | 用各种线型图案填充图形 |
纹理画刷 | TextureBrush | 使用基于光栅的图像填充图形 |
线型渐变画刷 | LinearGradientBrush | 使用渐变的色彩填充图形,渐变是指色彩沿着指定的方向在两个边界线之间变为另一种色彩 |
路径渐变画刷 | PathGradientBrush | 画刷沿着预定义的路径进行色彩渐变的填充,色彩渐变的方向是从路径的中心向边框渐变的 |
下面展示下他们的绘制效果:
Demo代码:
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(Color.White); Rectangle rect1 = new Rectangle(50,50,200,100); Pen p = new Pen(Color.Black,3); //单色画刷(SolidBrush) SolidBrush sb = new SolidBrush(Color.FromArgb(255,0,0)); //使用SolidBrush 画刷填充第一个矩形并绘制出rect1的边框 g.FillRectangle(sb,rect1); g.DrawRectangle(p, rect1); //============================================================================= Rectangle rect2 = new Rectangle(300,50,200,100); //影线画刷(HatchBrush) HatchBrush hb = new HatchBrush(HatchStyle.BackwardDiagonal,Color.FromArgb(255,0,0),Color.Gray); //使用 HatchBrush 画刷填充第二个矩形,并绘制出其边框 g.FillRectangle(hb,rect2); g.DrawRectangle(p,rect2); //============================================================================= Rectangle rect3 = new Rectangle(50,200,200,100); //纹理画刷(TextureBrush) Bitmap bmp = (Bitmap)Image.FromFile(@"C:\Users\ThinkPad\Desktop\GDIPuls Brush\Start.png"); TextureBrush tb = new TextureBrush(bmp); //绘制原图 g.DrawImage(bmp,50,320); //填充矩形3并绘制矩形边框 g.FillRectangle(tb,rect3); g.DrawRectangle(p,rect3); //=============================================================================== Rectangle rect4 = new Rectangle(300,200,200,100); //线性渐变画刷(LinearGradientBrush) LinearGradientBrush lb = new LinearGradientBrush(rect4,Color.Green,Color.Yellow,LinearGradientMode.ForwardDiagonal); //填充矩形4并绘制其边框 g.FillRectangle(lb,rect4); g.DrawRectangle(p,rect4); //=============================================================================== Rectangle rect5 = new Rectangle(550,200,200,100); //路径渐变画刷(PathGradientBrush) Point[] pts = { new Point(rect5.Left,rect5.Top), new Point(rect5.Right,rect5.Top), new Point(rect5.Right,rect5.Bottom), new Point(rect5.Left,rect5.Bottom) }; PathGradientBrush pb = new PathGradientBrush(pts); //设置中心的颜色 pb.CenterColor = Color.Green; //填充矩形5并绘制其边框 g.FillRectangle(pb,rect5); g.DrawRectangle(p,rect5); //释放所有资源 p.Dispose(); sb.Dispose(); hb.Dispose(); tb.Dispose(); lb.Dispose(); pb.Dispose(); }