Using Gradient Pens and Brushes

Pens and brushes have come a long way in a short time. GDI+ allows you to have lines and filled areas that show a gradient or sweep of colors. Modifying the code in Listing 3.5.1 will allow us to use the gradient pens or gradient fills instead of solid lines or colors.

To fill the background of the window is simple enough, we just need to specify a gradient fill brush. The LinearGradientBrush object is a member of the System.Drawing.Drawing2d namespace. Drawing a gradient fill on a line is very simple, because one of the overloads allows you to specify two Point objects, each at opposite corners of a rectangle.

Listing 3.5.2 is a selective listing of the two sections you need to change in the example from Listing ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.