System.Drawing.Drawing2D.LinearGradientBrush
https://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.lineargradientbrush(v=vs.110).aspx
横向渐变
LinearGradientBrush linGrBrush = new LinearGradientBrush( new Point(0, 10), new Point(200, 10), Color.FromArgb(255, 255, 0, 0), // Opaque red Color.FromArgb(255, 0, 0, 255)); // Opaque blue Pen pen = new Pen(linGrBrush); //public void DrawLine(Pen pen, int x1, int y1, int x2, int y2); e.Graphics.DrawLine(pen, 0, 10, 200, 10); //public void FillEllipse(Brush brush, int x, int y, int width, int height); //x,y是矩形的左上角的点的坐标,然后是宽和高。在矩形内部画椭圆 e.Graphics.FillEllipse(linGrBrush, 0, 30, 200, 100); //public void FillRectangle(Brush brush, int x, int y, int width, int height); e.Graphics.FillRectangle(linGrBrush, 0, 155, 500, 30);
new Point(0, 10), new Point(200, 10),
new Point(0, 10), new Point(100, 10),
纵向渐变
LinearGradientBrush linGrBrush = new LinearGradientBrush( new Point(0, 0), new Point(0, 30), Color.FromArgb(255, 255, 0, 0), // Opaque red Color.FromArgb(255, 0, 0, 255)); // Opaque blue Pen pen = new Pen(linGrBrush); //public void DrawLine(Pen pen, int x1, int y1, int x2, int y2); e.Graphics.DrawLine(pen, 100, 0, 100, 30); //public void FillEllipse(Brush brush, int x, int y, int width, int height); //x,y是矩形的左上角的点的坐标,然后是宽和高。在矩形内部画椭圆 e.Graphics.FillEllipse(linGrBrush, 0, 60, 200, 120); //public void FillRectangle(Brush brush, int x, int y, int width, int height); e.Graphics.FillRectangle(linGrBrush, 0, 210, 500, 30);
需要注意的是,纵向的渐变,是从坐标0开始的,而不是图形的0开始
假如最后一个矩形,左上角坐标改为(0,190),那么渐变的起始颜色,是从三分之一处开始的
如下图