C#LinearGradientBrush(渐变)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Drawing.Drawing2D; 10 11 namespace LinearGradientBrushDemo 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form1_Paint(object sender, PaintEventArgs e) 21 { 22 //绘画一个四边形 23 Graphics g = e.Graphics; 24 //DrawColorPolygon(g); 25 DrawButton(g); 26 } 27 28 private void DrawButton(Graphics g) 29 { 30 //绘画一个四边形 31 PointF[] _pointfs = new PointF[4]; 32 _pointfs[0] = new PointF(100, 50); 33 _pointfs[1] = new PointF(100, 80); 34 _pointfs[2] = new PointF(300, 80); 35 _pointfs[3] = new PointF(300, 50); 36 g.DrawPolygon(Pens.Black, _pointfs); 37 38 //设置渐变区域Rectangle 39 Rectangle _rect = new Rectangle(100, 50, 200, 30); 40 //设置渐变填充对象 41 LinearGradientBrush _linearGradientBrush = new LinearGradientBrush( 42 _rect, Color.FromArgb(60, 60, 60), Color.FromArgb(20, 20, 20), LinearGradientMode.Vertical); 43 44 //定义用于在多色渐变中以内插值取代颜色混合的颜色和位置的数组 45 ColorBlend _colorBlend = new ColorBlend(); 46 //定义多种颜色 47 _colorBlend.Colors = new Color[] { Color.FromArgb(60, 60, 60), Color.FromArgb(45, 45, 40), Color.FromArgb(20, 20, 20) }; 48 _colorBlend.Positions = new float[] { 0 / 3f, 1 / 2f, 3 / 3f }; 49 //设置多色渐变颜色 50 _linearGradientBrush.InterpolationColors = _colorBlend; 51 52 //填充多边形 53 g.FillPolygon(_linearGradientBrush, _pointfs); 54 //释放笔 55 _linearGradientBrush.Dispose(); 56 } 57 58 private void DrawColorPolygon(Graphics g) 59 { 60 //绘画一个四边形 61 PointF[] _pointfs = new PointF[4]; 62 63 _pointfs[0] = new PointF(100, 50); 64 _pointfs[1] = new PointF(50, 80); 65 _pointfs[2] = new PointF(200, 80); 66 _pointfs[3] = new PointF(150, 50); 67 68 g.DrawPolygon(Pens.Black, _pointfs); 69 70 //设置Rectangle 71 Rectangle _rect = new Rectangle(50, 50, 150, 30); 72 LinearGradientBrush _linearGradientBrush = new LinearGradientBrush( 73 _rect, Color.Red, Color.White, LinearGradientMode.Horizontal); 74 75 //定义用于在多色渐变中以内插值取代颜色混合的颜色和位置的数组 76 ColorBlend _colorBlend = new ColorBlend(); 77 //定义多种颜色 78 _colorBlend.Colors = new Color[] 79 {Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, 80 Color.Aquamarine, Color.Azure,Color.Beige}; 81 _colorBlend.Positions = new float[] { 82 0/5f,1 / 5f, 2 / 5f, 3 / 5f, 4 / 5f, 5 / 5f 83 }; 84 //设置多色渐变颜色 85 _linearGradientBrush.InterpolationColors = _colorBlend; 86 87 //填充多边形 88 g.FillPolygon(_linearGradientBrush, _pointfs); 89 _linearGradientBrush.Dispose(); 90 } 91 } 92 }
LinearGradientBrush 使用线性渐变绘制区域。 线性渐变沿直线定义渐变。 该直线的终点由线性渐变的 StartPoint 和 EndPoint 属性定义。 LinearGradientBrush 画笔沿此直线绘制其 GradientStops。
默认的线性渐变是沿对角方向进行的。 默认情况下,线性渐变的 StartPoint 是被绘制区域的左上角值为 0,0 的 Point,其 EndPoint 是被绘制区域的右下角值为 1,1 的 Point。 所得渐变的颜色是沿着对角方向路径插入的。
下图演示对角渐变。 其中添加了一条线,用于突出显示渐变从起点到终点的内插路径。
对角方向的线性渐变

下一幅插图显示的是同一线性渐变,但它具有突出显示的渐变停止点。
具有突出显示的渐变停止点的对角线性渐变

可以指定未完全填充所绘制区域的渐变轴。 出现这种情况时,SpreadMethod 属性确定其余区域的绘制方式。
浙公网安备 33010602011771号