Files
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

11 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create a Path Gradient 03/30/2017
csharp
vb
path gradients [Windows Forms], creating
gradients [Windows Forms], creating path
graphics paths [Windows Forms], creating gradient
1948e834-e104-481c-b71d-d8aa9e4d106e

How to: Create a Path Gradient

The xref:System.Drawing.Drawing2D.PathGradientBrush class allows you to customize the way you fill a shape with gradually changing colors. For example, you can specify one color for the center of a path and another color for the boundary of a path. You can also specify separate colors for each of several points along the boundary of a path.

Note

In GDI+, a path is a sequence of lines and curves maintained by a xref:System.Drawing.Drawing2D.GraphicsPath object. For more information about GDI+ paths, see Graphics Paths in GDI+ and Constructing and Drawing Paths.

The examples in this article are methods that are called from a control's xref:System.Windows.Forms.Control.Paint event handler.

To fill an ellipse with a path gradient

To specify points on the boundary

To customize a path gradient

  • One way to customize a path gradient brush is to set its xref:System.Drawing.Drawing2D.PathGradientBrush.FocusScales%2A property. The focus scales specify an inner path that lies inside the main path. The center color is displayed everywhere inside that inner path rather than only at the center point.

    The following example creates a path gradient brush based on an elliptical path. The code sets the boundary color to blue, sets the center color to aqua, and then uses the path gradient brush to fill the elliptical path.

    Next, the code sets the focus scales of the path gradient brush. The x focus scale is set to 0.3, and the y focus scale is set to 0.8. The code calls the xref:System.Drawing.Graphics.TranslateTransform%2A method of a xref:System.Drawing.Graphics object so that the subsequent call to xref:System.Drawing.Graphics.FillPath%2A fills an ellipse that sits to the right of the first ellipse.

    To see the effect of the focus scales, imagine a small ellipse that shares its center with the main ellipse. The small (inner) ellipse is the main ellipse scaled (about its center) horizontally by a factor of 0.3 and vertically by a factor of 0.8. As you move from the boundary of the outer ellipse to the boundary of the inner ellipse, the color changes gradually from blue to aqua. As you move from the boundary of the inner ellipse to the shared center, the color remains aqua.

    The following illustration shows the output of the following code. The ellipse on the left is aqua only at the center point. The ellipse on the right is aqua everywhere inside the inner path.

Gradient effect of focus scales

[!code-csharpSystem.Drawing.UsingaGradientBrush#14] [!code-vbSystem.Drawing.UsingaGradientBrush#14]

To customize with interpolation

  • Another way to customize a path gradient brush is to specify an array of interpolation colors and an array of interpolation positions.

    The following example creates a path gradient brush based on a triangle. The code sets the xref:System.Drawing.Drawing2D.PathGradientBrush.InterpolationColors%2A property of the path gradient brush to specify an array of interpolation colors (dark green, aqua, blue) and an array of interpolation positions (0, 0.25, 1). As you move from the boundary of the triangle to the center point, the color changes gradually from dark green to aqua and then from aqua to blue. The change from dark green to aqua happens in 25 percent of the distance from dark green to blue.

    The following illustration shows the triangle filled with the custom path gradient brush.

    Triangle filled with custom path gradient brush.

    [!code-csharpSystem.Drawing.UsingaGradientBrush#15] [!code-vbSystem.Drawing.UsingaGradientBrush#15]

To set the center point

Compiling the Code

The preceding examples are designed for use with Windows Forms, and they require xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of the xref:System.Windows.Forms.Control.Paint event handler.

See also