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,
    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[] 
    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 Example   Follow me on Twitter   RSS feed

This entry was posted in drawing, graphics and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Use a LinearGradientBrush in C#

  1. Martin H says:

    Good article.
    I think there is a little fault. Gradient from color orange to yellow is not looking good.
    In colorBlend.Positions array the value 1/6f is twice. I changed the second one to 2/6f.
    Gradient from orange to yellow looks then better.

  2. Rod Stephens says:

    Oops! You’re right. Thanks for pointing this out. I’ve fixed the example.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.