views:

292

answers:

2

Is there a way to draw a line along a curved path with a gradient that varies in a direction perpendicular to the direction of the line? I am using the GDI+ framework for my graphics.

+1  A: 

The simple answer is no. You can create a GraphicsPath in order to describe what you would like to draw, using AddPoint/AddLine/AddBezier and so forth as needed to describe the complex path of what you want to draw. When you draw the path you can provide a Brush which can be something like LinearGradientBrush or RadialGradientBrush. Neither of those gradient brushes reacts to the actual path being drawn in the sense of changing direction as the drawing occurs. You have to specify the angles etc as constant for the entire gradient area.

Phil Wright
A: 

One possible method you can use is to set the clip region of the Graphics object to be that of the line only. Then draw a Linear Gradient over the extremes of the line e.g.

GraphicsPath gp = new GraphicsPath();

gp.AddArc(); // etc...

graphics.SetClip( gp );

graphics.FillRectangle( myLinearGradientBrush, gp.GetBounds());

The above code might give you what you are looking for.

TK