C# 用渐变色画一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/576861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:57:37  来源:igfitidea点击:

Drawing a line with a gradient color

c#winformsgdi+linegradient

提问by TK.

Is it possible to draw a line using a graduated colour?

是否可以使用渐变色绘制线条?

I want to be able to draw a straight or a curved line (if possible) where at one end of the line is Blue and the other end is Red.

我希望能够画一条直线或曲线(如果可能的话),线的一端是蓝色,另一端是红色。

Further There might be a need to have more than one gradient per-line e.g the colour going from Blue -> Green -> Red. I am thinking that this might just consist of multiple gradient lines drawn together.

此外,每行可能需要有多个渐变,例如颜色从蓝色 -> 绿色 -> 红色。我认为这可能只是由绘制在一起的多条渐变线组成。

采纳答案by Mitch Wheat

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics graphicsObject = e.Graphics;

    using (Brush aGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(50, 0), Color.Blue, Color.Red))
    {
        using (Pen aGradientPen = new Pen(aGradientBrush))
        {
            graphicsObject.DrawLine(aGradientPen, new Point(0, 10), new Point(100, 10));
        }
    }
}

回答by lubos hasko

you will need to use System.Drawing.Drawing2D.LinearGradientBrushinstead of System.Drawing.SolidBrush

你将需要使用System.Drawing.Drawing2D.LinearGradientBrush而不是System.Drawing.SolidBrush

example:

例子:

e.Graphics.DrawLine(new Pen(new System.Drawing.Drawing2D.LinearGradientBrush(...