一,solidBrush
纯色填充
利用窗体的paint事件
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Brush br = new SolidBrush(Color.Red); g.FillRectangle(br,10,10,100,100); g.Dispose(); }
二,TextureBrush
利用位图填充
首先添加一张图片
using System.IO; private void Form1_Paint(object sender, PaintEventArgs e) { string path = @"F:\Lianxi\App1\App1\Img\龙猫.jpg"; Graphics g = e.Graphics; Bitmap img; if (File.Exists(path)) { img = new Bitmap(path); Brush br = new TextureBrush(img); g.FillRectangle(br, 30, 30, 500, 400); } else { MessageBox.Show("找不到要填充的图片","提示",MessageBoxButtons.OK); } g.Dispose();//释放Graphics所使用的资源 }
三,LinearGradientBrush
线性渐变
using System.Drawing.Drawing2D; private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 140), new Point(280, 140), Color.Red, Color.White); g.FillEllipse(lgb, 0, 140, 280, 120); lgb.Dispose(); g.Dispose(); }
四,PathGradientBrush
中心点渐变
using System.Drawing.Drawing2D; private void Form1_Paint(object sender, PaintEventArgs e) { GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 80, 280, 120); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.FromArgb(0, 255, 255, 20); Color[] colors = { Color.FromArgb(255, 0, 255, 0)};//FromArgb(透明度,R,G,B) pgb.SurroundColors = colors; e.Graphics.FillEllipse(pgb, 0, 80, 280, 120); pgb.Dispose(); }
五,HatchBrush
以条案填充
using System.Drawing.Drawing2D; private void Form1_Paint(object sender, PaintEventArgs e) { HatchBrush hatchBrush = new HatchBrush(HatchStyle.HorizontalBrick,Color.White,Color.Red); e.Graphics.FillRectangle(hatchBrush,10,10,100,100); e.Graphics.Dispose(); }