Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 887
TK. Avatar asked Feb 23 '09 08:02

TK.


People also ask

How do you make a gradient color in sketch?

To change a color on your gradient, click on either point and select a color using the Color popover in the Inspector. To add another color to your gradient, click anywhere on the gradient line to add a new point. Drag the color points to change the look of your gradient.

How do you make a color gradient in Word?

On the Format tab, click Shape Fill, click Gradient, and select the one you want. Tip: To quickly apply this same gradient fill to other shapes, use the Format Painter.


2 Answers

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));
        }
    }
}
like image 106
Mitch Wheat Avatar answered Sep 22 '22 10:09

Mitch Wheat


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(...
like image 35
lubos hasko Avatar answered Sep 22 '22 10:09

lubos hasko