Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/brushes-and-filled-shapes-in-gdi.md
T
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

5.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Brushes and Filled Shapes in GDI+ 03/30/2017
csharp
vb
brushes [Windows Forms], GDI+
filled shapes [Windows Forms], GDI+
shapes [Windows Forms], GDI+
GDI+, brushes
GDI+, filled shapes
gradient brushes
brushes [Windows Forms], gradient
e863e2a7-0294-4130-99b6-f1ea3201e7cd

Brushes and Filled Shapes in GDI+

A closed shape, such as a rectangle or an ellipse, consists of an outline and an interior. The outline is drawn with a pen and the interior is filled with a brush. GDI+ provides several brush classes for filling the interiors of closed shapes: xref:System.Drawing.SolidBrush, xref:System.Drawing.Drawing2D.HatchBrush, xref:System.Drawing.TextureBrush, xref:System.Drawing.Drawing2D.LinearGradientBrush, and xref:System.Drawing.Drawing2D.PathGradientBrush. All of these classes inherit from the xref:System.Drawing.Brush class. The following illustration shows a rectangle filled with a solid brush and an ellipse filled with a hatch brush.

Filled Shapes

Solid Brushes

To fill a closed shape, you need an instance of the xref:System.Drawing.Graphics class and a xref:System.Drawing.Brush. The instance of the xref:System.Drawing.Graphics class provides methods, such as xref:System.Drawing.Graphics.FillRectangle%2A and xref:System.Drawing.Graphics.FillEllipse%2A, and the xref:System.Drawing.Brush stores attributes of the fill, such as color and pattern. The xref:System.Drawing.Brush is passed as one of the arguments to the fill method. The following code example shows how to fill an ellipse with a solid red color.

[!code-csharpLinesCurvesAndShapes#121] [!code-vbLinesCurvesAndShapes#121]

Note

In the preceding example, the brush is of type xref:System.Drawing.SolidBrush, which inherits from xref:System.Drawing.Brush.

Hatch Brushes

When you fill a shape with a hatch brush, you specify a foreground color, a background color, and a hatch style. The foreground color is the color of the hatching.

[!code-csharpLinesCurvesAndShapes#122] [!code-vbLinesCurvesAndShapes#122]

GDI+ provides more than 50 hatch styles; the three styles shown in the following illustration are xref:System.Drawing.Drawing2D.HatchStyle.Horizontal, xref:System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, and xref:System.Drawing.Drawing2D.HatchStyle.Cross.

Filled Shapes

Texture Brushes

With a texture brush, you can fill a shape with a pattern stored in a bitmap. For example, suppose the following picture is stored in a disk file named MyTexture.bmp.

Filled Shape

The following code example shows how to fill an ellipse by repeating the picture stored in MyTexture.bmp.

[!code-csharpLinesCurvesAndShapes#123] [!code-vbLinesCurvesAndShapes#123]

The following illustration shows the filled ellipse.

Filled Shape

Gradient Brushes

GDI+ provides two kinds of gradient brushes: linear and path. You can use a linear gradient brush to fill a shape with color that changes gradually as you move across the shape horizontally, vertically, or diagonally. The following code example shows how to fill an ellipse with a horizontal gradient brush that changes from blue to green as you move from the left edge of the ellipse to the right edge.

[!code-csharpLinesCurvesAndShapes#124] [!code-vbLinesCurvesAndShapes#124]

The following illustration shows the filled ellipse.

Filled Shape

A path gradient brush can be configured to change color as you move from the center of a shape toward the edge.

Filled Shape

Path gradient brushes are quite flexible. The gradient brush used to fill the triangle in the following illustration changes gradually from red at the center to each of three different colors at the vertices.

Filled Shape

See also