[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use a LinearGradientBrush in C#


A LinearGradientBrush fills an area with a color gradient that blends smoothly from one color to another.

The LinearGradientBrush class has a Dispose method that you should call when you're done with the brush. To make that easier, place the brush in a using block so Dispose is called automatically when the block ends.

The class has many different constructors, three of which are demonstrated by the example program. The following code uses one of the simpler, defining the brush by specifying a start point, end point, start color, and end color.

// Define a brush with two points and their colors. using (LinearGradientBrush br = new LinearGradientBrush( new Point(10, 10), new Point(140, 50), Color.Red, Color.White)) { e.Graphics.FillRectangle(br, 10, 10, 125, 50); e.Graphics.DrawRectangle(Pens.Black, 10, 10, 125, 50); }

This is somewhat awkward because you specify the brush using points but you specify the areas to draw with a point, width, and height.

The following code uses the same Rectangle to specify the brush's area and the area to be drawn. The final parameter to the brush's constructor gives the direction in which the colors should flow. This can be BackwardDiagonal, ForwardDiagonal, Horizontal, or Vertical. Alternatively you can specify the angle in which the colors should flow.

// Define a brush with a Rectangle, colors, and gradient mode. Rectangle rect = new Rectangle(145, 10, 125, 50); using (LinearGradientBrush br = new LinearGradientBrush( rect, Color.Blue, Color.White, LinearGradientMode.ForwardDiagonal)) { e.Graphics.FillRectangle(br, rect); e.Graphics.DrawRectangle(Pens.Black, rect); }

The final example in this program uses a gradient that flows between more than two colors.

// Define a gradient with more than 2 colors. rect = new Rectangle(10, 70, 260, 50); using (LinearGradientBrush br = new LinearGradientBrush( rect, Color.Blue, Color.White, 0f)) { // Create a ColorBlend object. Note that you // must initialize it before you save it in the // brush's InterpolationColors property. ColorBlend colorBlend = new ColorBlend(); colorBlend.Colors = new Color[] { Color.Red, Color.Orange, Color.Yellow, Color.Lime, Color.Blue, Color.Indigo, Color.Violet, }; colorBlend.Positions = new float[] { 0f, 1/6f, 2/6f, 3/6f, 4/6f, 5/6f, 1f }; br.InterpolationColors = colorBlend; e.Graphics.FillRectangle(br, rect); e.Graphics.DrawRectangle(Pens.Black, rect); }

This code creates the brush specifying a Rectangle that it should cover, two colors, and the angle 0 degrees (so this is a horizontal gradient).

It then creates a ColorBlend object to represent the brush's colors. It initializes the object's Colors property to an array of Color values. It initializes the Positions property to an array of floats between 0 and 1 that indicate where inside the brush the different colors should appear.

Finally the code sets the brush's InterpolationColors property to the ColorBlend object. Note that you must initialize the ColorBlend before you do this or the program will crash.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.