Drawing a line with a gradient color

隐身守侯 提交于 2020-01-21 07:08:05

问题


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.


回答1:


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));
        }
    }
}



回答2:


you will need to use System.Drawing.Drawing2D.LinearGradientBrush instead of System.Drawing.SolidBrush

example:

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


来源:https://stackoverflow.com/questions/576861/drawing-a-line-with-a-gradient-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!