Simple Gradient

A simple was how to draw a gradient on a form. All you need is to include both Drawing and Drawing2D namespaces in your project. As we are going to draw onto the form, we need to assure, that if user resizes it, we will have our gradient spread over the whole form. We can do this by using form’s Resize event. Each time the form is being resized, we tell it to repaint.

In order to paint on the form, we are going to use its Paint event. In this way, the next time a message to repaint is received, we will have our gradient repainted.

The class LinearGradientBrush creates a special brush, which we can use in our further work. It accepts four arguments:

  1. a rectangle, which determines the area where we are going to paint
  2. color one
  3. color two
  4. an angle

Here is a simple example.