简体   繁体   中英

Drawing a line with a gradient color

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 eg the colour going from Blue -> Green -> Red. I am thinking that this might just consist of multiple gradient lines drawn together.

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

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(...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM