views:

2305

answers:

3

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

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

Further There might be a beed to have more than one gradient per-line e.g the colour going from blue -> green -> red. Im thinking that this might just consist of mulitlpe gradiated lines drawn together.

+3  A: 

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(...
lubos hasko
+2  A: 

Try looking at the LinearGradientBrush

Ed Swangren
+3  A: 
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));
        }
    }
}
Mitch Wheat
You should be calling dispose...
Ed Swangren
no, he doesn't need to in this case. Dispose() method will be called automatically after OnPaint method.
lubos hasko
He should, on the paint Pen and Brush. Better yet, keep them alive as members and don't create new ones on each paint.
configurator
thanks guys. I hastily put the snippet together, which is my bad. Have fixed up.
Mitch Wheat
Yes, I meant on the brush and pen. Didn't mean to get overly anal...
Ed Swangren