C#中提供了好几种画刷的使用,最近一直在用这个,今天就总结一下这几种画刷的使用,便于以后查看!
1. 创建透明画刷
Size size = this.Size;
Point pt = this.Location;
//创建透明画刷
Color tcolor = Color.FromArgb(80, 222, 252, 249);
Brush tbrush = new SolidBrush(tcolor);
Rectangle trect = new Rectangle(pt.X, pt.Y, size.Width, 20);
Brush fontbrush = new SolidBrush(Color.Black);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y));
e.Graphics.FillRectangle(tbrush, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
2. 创建线性渐变色画刷(接第一步的代码)
//创建线性渐变色画刷
trect.Y += 30;
LinearGradientBrush lgbrush = new LinearGradientBrush(trect,
Color.FromArgb(222, 200, 249),
Color.FromArgb(222, 252, 249),
LinearGradientMode.Vertical);
e.Graphics.FillRectangle(lgbrush, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y + 30));
3. 创建普通画刷(接第二步的代码)
//创建普通画刷
trect.Y += 30;
Brush brush = new SolidBrush(Color.FromArgb(222, 252, 249));
e.Graphics.FillRectangle(brush, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y + 60));
4. 创建阴影画刷(接第三步的代码)
//创建阴影画刷
trect.Y += 30;
Brush hbrush = new HatchBrush(HatchStyle.DarkVertical, Color.FromArgb(222, 200, 249), Color.FromArgb(222, 252, 249));
e.Graphics.FillRectangle(hbrush, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y + 90));
5. 创建纹理画刷(接第四步的代码)
//创建纹理画刷
trect.Y += 30;
string path = "C:\\1.jpg";
Image image = Image.FromFile(path);
Brush txbursh = new TextureBrush(image, WrapMode.Tile);
e.Graphics.FillRectangle(txbursh, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y + 120));
6. 创建路径画刷(接第五步的代码)
//创建路径画刷
trect.Y += 30;
Point[] points = { new Point(pt.X, pt.Y + 150),
new Point(pt.X + size.Width, pt.Y + 150),
new Point(pt.X + size.Width, pt.Y + 170),
new Point(pt.X, pt.Y + 170)};
PathGradientBrush pgbrush = new PathGradientBrush(points);
e.Graphics.FillRectangle(pgbrush, trect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(222, 252, 249), 1), trect);
e.Graphics.DrawString("This is test Font", new Font("宋体", 12), fontbrush, new Point(pt.X, pt.Y + 150));
7. 看看编译后的效果图(从上至下按照上面的顺序进行排列)