创建Brush对象

在GDI+中,可使用笔刷,以各种个颜色和图像填充图形,GDI+的Brush类本身是一个抽象的类,所以是不能实例化Brush的

但是GDI+的API提供五个类,就扩展了Brush类并提供了具体的实现方式

SolidBrush        使用纯颜色填充图形

TextureBrush       使用基于光栅的图像(位图,JPG等图像)填充图形

LinearGradientBrush    使用颜色渐变填充图形

PathGradientBrush     使用渐变色填充图形,渐变方向是从有路径定义的图像便捷指                                                      向 图形的中心

HatchBrush        使用各类图案填充图形

1.solidBrush实例

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //定义红色笔刷  brush为抽象类所以不能实例化
            Brush sh = new SolidBrush(Color.Red);
            //填充椭圆
            g.FillEllipse(sh,0,80,245,200);
        }    
//用户定义单色画笔,画笔用于填充图形形状,如矩形 椭圆,扇形,多边形和封闭路径

2.TextureBrush实例

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Image a = Image.FromFile(@"d:my documentsvisual studio 2013Projects13.3Brush五大API13.3Brush五大APIImage2323.jpg");
            Graphics g = e.Graphics;
            Brush sh = new TextureBrush(a);
            g.FillEllipse(sh,0,80,445,200);
            //释放资源
            g.Dispose();
        }

3.LineargradientBrush

  使用颜色的线性渐变填充图形,如白色渐变到黑色。在使用此类前要先引入System.Drawing.Drawing2D命名空间

GDI+提供水平、垂直、和对角线方向线性渐变。在默认情况下,线性渐变中的颜色均匀地变化。当然,也可自定义线性渐变,使颜色非均匀变化。

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //定义渐变线行=2个点2个颜色
            LinearGradientBrush gh = new LinearGradientBrush(new Point(0, 80), new Point(280, 120), Color.Red, Color.Green);
            Brush sh = gh;
            g.FillEllipse(sh,0,80,240,240);
       g.Dispose(); }

4.PathGradientBrush

  使用户可以自定义用渐变色填充形状的方式。在GDI+中,路径是由GraphicsPath对象维护的一系列线条和曲线。在使用此类前腰引入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(255,255,0,0);
            //定义color数组来接收
            Color[] ll = {Color.FromArgb(255,255,0,0) };
            //填充中心点对应的数组
            pgb.SurroundColors = ll;
            //填充椭圆
            e.Graphics.FillEllipse(pgb,0,80,280,120);
            pgb.Dispose();        
        }

5.HatchBrush

  阴影图案由两种颜色组成:一种背景颜色,一种是在背景上形成图案的线条的颜色,若要用阴影图案填充闭合的形状,需使用HatchBrush类的对象。

在使用此类前要先引入System.Drawing.Drawing2D命名空间。

//定义阴影样式
HatchBrush hat = new HatchBrush(HatchStyle.Horizontal,Color.Yellow,Color.Red);
//定义画刷
            Brush gr = hat;
//填充
            e.Graphics.FillEllipse(gr,0,80,280,240);
//释放
            e.Dispos();

  

  

原文地址:https://www.cnblogs.com/xiaowie/p/8891476.html