Andres Jaimes

How to create a simple gradient in C#

By Andres Jaimes

Creating a simple gradient using two colors is very simple:

Rectangle rect = new Rectangle(0, 0, 100, 100);
LinearGradientBrush lgb = new LinearGradientBrush(rect, Color.White, Color.Blue, 90);
e.Graphics.FillRectangle(lgb, rect);

This code will generate a gradient in the given rectangle that goes from white to blue in a vertical way (see last parameter in constructor). In this case, e, is any class that encapsulates the System.Drawing.Graphics class. Once the gradient is ready, well, you have to actually draw it by calling the FillRectangle function.

You can use this gradient for images or printings or any other place where a regular Brush may fit.