tags:

views:

1453

answers:

2

Is there a method I can use to generate non-linear Gradient brushes in c#?

To clarify: Im not looking for a gradient that can be applied to a non-linear path. Rather, I am looking for a gradient between 2 colours that fades between the two in a non-linear fashion i.e. the rate of changs is quicker at the start of the gradient and slows down as the brush nears the finishing point.

Is this even remotly possible in c# using gdi+?

+5  A: 

Yep, set the blend for the Brush.

LinearGradientBrush blendBrush = new LinearGradientBrush(Rectangle, Color.Red, Color.White, 360f);
Blend blend = new Blend();
blend .Factors = new float[] { 1.0f, 0.9f, 0.8f, 0.7f, 0.6f, 0.5f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f };
blend .Positions = new float[] { 0, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f };
blendBrush.Blend = blend ;

This example will start fading out, then go back to the previous colour. So a kinda fill from centre effect.

Ian
Thanks, this does the trick! Now to programatically calculate a non-linear blend!
TK
+1  A: 

Use the Blend property of LinearGradientBrush -

http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.blend(VS.80).aspx

Alex