Brush笔刷类,可以用颜色和图像填充图形,是 抽象类,不可以实例化。
实例:
1、SolidBrushTest
using System; using System.Drawing; using System.Windows.Forms; namespace SolidBrushTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Brush brush = new SolidBrush(Color.Orange); g.FillEllipse(brush, 10, 10, 200, 120); g.Dispose(); } } }
2、 TextureBrushTest
using System; using System.Drawing; using System.Windows.Forms; using System.IO; namespace TextureBrushTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { string path = @"D:\CS\GDIPlusTest\TextureBrushTest\img\微信图片_20170817213231.jpg"; Graphics g=e.Graphics; if (File.Exists(path)) { Bitmap map = new Bitmap(path); Brush brush = new TextureBrush(map); g.FillEllipse(brush, 10, 10, 500, 500); brush.Dispose(); } else { MessageBox.Show("image is not exists"); } g.Dispose(); } } }
3、LinearGradientBrushTest
using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace LinearGradientBrushTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; LinearGradientBrush lgb = new LinearGradientBrush(new Point(10,10),new Point(290,90),Color.White,Color.FromArgb(255,0,0,0)); g.FillEllipse(lgb,10,10,280,120); lgb.Dispose(); g.Dispose(); } } }
4、 PathGradientBrushTest
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace PathGradientBrushTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { //绘画路径 GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0,80,240,120); //路径渐变画刷 PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.Orange; Color[] colors = { Color.FromArgb(255,0,255,0)}; pgb.SurroundColors = colors; //绘制椭圆 e.Graphics.FillEllipse(pgb,0,80,240,120); pgb.Dispose(); } } }
5、 HatchBrushTest
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace HatchBrushTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { HatchBrush hatchBrush = new HatchBrush(HatchStyle.HorizontalBrick,Color.Red,Color.Yellow); e.Graphics.FillEllipse(hatchBrush,0,80,240,120); hatchBrush.Dispose(); } } }