10.5 Using Gradient Fills

 <  Day Day Up  >  

You want to create a shape that uses a gradient fill.


Technique

Create a LinearGradientBrush using two Point objects or a single Rectangle object, a starting and ending Color value, and an optional orientation angle.

 
 brush = new LinearGradientBrush(new Rectangle(new Point(0,0), new Size(panel1.Width, graphics/ccc.gif panel1.Height)),                 Color.White, Color.Black, LinearGradientMode.ForwardDiagonal ); 

You can also control how the color blend is performed by setting the Blend property of the LinearGradientBrush object. A Blend object contains a Positions array and a Factors array, both of which are float arrays. Each value in the Positions array defines a percentage of the gradient fill area. Each value in the Factors array represents the percentage up to the point of the corresponding position value that consists of the starting color:

 
 gradientBlend = new Blend(3); gradientBlend.Positions[0] = gradientBlend.Factors[0] = 0.0f; gradientBlend.Positions[1] = gradientBlend.Factors[1] = 0.4f; gradientBlend.Positions[2] = gradientBlend.Factors[2] = 1.0f; brush.Blend = gradientBlend; 

Comments

To fill a shape using a gradient fill method, use a LinearGradientBrush object. There are several constructors available to use when creating the brush. You can specify two Point structures or a single Rectangle that corresponds to the starting and ending points of the area to fill with the gradient. Any other area outside of those areas is filled using a solid brush. Each gradient brush also uses two Color values corresponding to the starting and ending color. The brush interpolates the colors in between those two values to create the gradient fill. Finally, you can specify the orientation of the gradient fill either by passing a value from the LinearGradientMode enumerated data type or by passing a float value denoting the orientation angle. When you specify an orientation angle, the gradient brush draws each line of the fill perpendicular to the orientation angle. For instance, if the angle is 0, then the gradient fill is horizontal across the drawing area, as shown in Figure 10.2.

Figure 10.2. A LinearGradientBrush with an orientation angle of 0 degrees renders itself horizontally across the rendering surface.

graphics/10fig02.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net