* net-current-v30plus-md.md * net-current-v40plus-md.md * tla2sharptla-ui-md.md * tla2sharptla-uiautomation-md.md * tla2sharptla-winclient-md.md * tla2sharptla-xaml-md.md * tlasharptla-ui-md.md * tlasharptla-uiautomation-md.md * tlasharptla-winclient-md.md * tlasharptla-xaml-md.md * fix warnings
16 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||
|---|---|---|---|---|---|---|---|---|
| Brushes overview | Discover how to illustrate concepts by painting with simple, solid colors through gradients and patterns in Windows Presentation Foundation (WPF). | 03/30/2017 |
|
|
ecea1955-420b-45c6-bf43-c1404c072c41 |
WPF Brushes Overview
Everything visible on your screen is visible because it was painted by a brush. For example, a brush is used to describe the background of a button, the foreground of text, and the fill of a shape. This topic introduces the concepts of painting with user interface (UI) objects with anything from simple, solid colors to complex sets of patterns and images.
Painting with a Brush
A xref:System.Windows.Media.Brush "paints" an area with its output. Different brushes have different types of output. Some brushes paint an area with a solid color, others with a gradient, pattern, image, or drawing. The following illustration shows examples of each of the different xref:System.Windows.Media.Brush types.
Most visual objects enable you to specify how they are painted. The following table lists some common objects and properties with which you can use a xref:System.Windows.Media.Brush.
The following sections describe the different xref:System.Windows.Media.Brush types and provide an example of each.
Paint with a Solid Color
A xref:System.Windows.Media.SolidColorBrush paints an area with a solid xref:System.Windows.Media.Color. There are a variety of ways to specify the xref:System.Windows.Media.SolidColorBrush.Color%2A of a xref:System.Windows.Media.SolidColorBrush: for example, you can specify its alpha, red, blue, and green channels or use one of the predefined color provided by the xref:System.Windows.Media.Colors class.
The following example uses a xref:System.Windows.Media.SolidColorBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a SolidColorBrush
[!code-csharpBrushesIntroduction_snip#GraphicsMMSolidColorBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMSolidColorBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMSolidColorBrushExampleInline]
For more information about the xref:System.Windows.Media.SolidColorBrush class, see Painting with Solid Colors and Gradients Overview.
Paint with a Linear Gradient
A xref:System.Windows.Media.LinearGradientBrush paints an area with a linear gradient. A linear gradient blends two or more colors across a line, the gradient axis. You use xref:System.Windows.Media.GradientStop objects to specify the colors in the gradient and their positions.
The following example uses a xref:System.Windows.Media.LinearGradientBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a LinearGradientBrush
[!code-csharpBrushesIntroduction_snip#GraphicsMMLinearGradientBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMLinearGradientBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMLinearGradientBrushExampleInline]
For more information about the xref:System.Windows.Media.LinearGradientBrush class, see Painting with Solid Colors and Gradients Overview.
Paint with a Radial Gradient
A xref:System.Windows.Media.RadialGradientBrush paints an area with a radial gradient. A radial gradient blends two or more colors across a circle. As with the xref:System.Windows.Media.LinearGradientBrush class, you use xref:System.Windows.Media.GradientStop objects to specify the colors in the gradient and their positions.
The following example uses a xref:System.Windows.Media.RadialGradientBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a RadialGradientBrush
[!code-csharpBrushesIntroduction_snip#GraphicsMMRadialGradientBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMRadialGradientBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMRadialGradientBrushExampleInline]
For more information about the xref:System.Windows.Media.RadialGradientBrush class, see Painting with Solid Colors and Gradients Overview.
Paint with an Image
An xref:System.Windows.Media.ImageBrush paints an area with a xref:System.Windows.Media.ImageSource.
The following example uses an xref:System.Windows.Media.ImageBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a Image
[!code-csharpBrushesIntroduction_snip#GraphicsMMImageBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMImageBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMImageBrushExampleInline]
For more information about the xref:System.Windows.Media.ImageBrush class, see Painting with Images, Drawings, and Visuals.
Paint with a Drawing
A xref:System.Windows.Media.DrawingBrush paints an area with a xref:System.Windows.Media.Drawing. A xref:System.Windows.Media.Drawing can contain shapes, images, text, and media.
The following example uses a xref:System.Windows.Media.DrawingBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a DrawingBrush
[!code-csharpBrushesIntroduction_snip#GraphicsMMDrawingBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMDrawingBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMDrawingBrushExampleInline]
For more information about the xref:System.Windows.Media.DrawingBrush class, see Painting with Images, Drawings, and Visuals.
Paint with a Visual
A xref:System.Windows.Media.VisualBrush paints an area with a xref:System.Windows.Media.Visual object. Examples of Visual objects include xref:System.Windows.Controls.Button, xref:System.Windows.Controls.Page, and xref:System.Windows.Controls.MediaElement. A xref:System.Windows.Media.VisualBrush also enables you to project content from one portion of your application into another area; it's very useful for creating reflection effects and magnifying portions of the screen.
The following example uses a xref:System.Windows.Media.VisualBrush to paint the xref:System.Windows.Shapes.Shape.Fill%2A of a xref:System.Windows.Shapes.Rectangle. The following illustration shows the painted rectangle.

A Rectangle painted using a VisualBrush
[!code-csharpBrushesIntroduction_snip#GraphicsMMVisualBrushExampleInline] [!code-vbBrushesIntroduction_snip#GraphicsMMVisualBrushExampleInline] [!code-xamlBrushesIntroduction_snip#GraphicsMMVisualBrushExampleInline]
For more information about the xref:System.Windows.Media.VisualBrush class, see Painting with Images, Drawings, and Visuals.
Paint using Predefined and System Brushes
For convenience, Windows Presentation Foundation (WPF) provides a set of predefined and system brushes that you can use to paint objects.
-
For a list of available predefined brushes, see the xref:System.Windows.Media.Brushes class. For an example showing how to use a predefined brush, see Paint an Area with a Solid Color.
-
For a list of available system brushes, see the xref:System.Windows.SystemColors class. For an example, see Paint an Area with a System Brush.
Common Brush Features
xref:System.Windows.Media.Brush objects provide an xref:System.Windows.Media.Brush.Opacity%2A property that can be used to make a brush transparent or partially transparent. An xref:System.Windows.Media.Brush.Opacity%2A value of 0 makes a brush completely transparent, while an xref:System.Windows.Media.Brush.Opacity%2A value of 1 makes a brush completely opaque. The following example uses the xref:System.Windows.Media.Brush.Opacity%2A property to make a xref:System.Windows.Media.SolidColorBrush 25 percent opaque.
[!code-xamlBrushOverviewExamples_snip#OpacityExample1XAML]
[!code-csharpBrushOverviewExamples_snip#OpacityExample1CSharp]
If the brush contains colors that are partially transparent, the opacity value of the color is combined through multiplication with the opacity value of the brush. For example, if a brush has an opacity value of 0.5 and a color used in the brush also has an opacity value of 0.5, the output color has an opacity value of 0.25.
Note
It's more efficient to change the opacity value of a brush than it is to change the opacity of an entire element using its xref:System.Windows.UIElement.Opacity%2A?displayProperty=nameWithType property.
You can rotate, scale, skew, and translate a brush's content by using its xref:System.Windows.Media.Brush.Transform%2A or xref:System.Windows.Media.Brush.RelativeTransform%2A properties. For more information, see Brush Transformation Overview.
Because they are xref:System.Windows.Media.Animation.Animatable objects, xref:System.Windows.Media.Brush objects can be animated. For more information, see Animation Overview.
Freezable Features
Because it inherits from the xref:System.Windows.Freezable class, the xref:System.Windows.Media.Brush class provides several special features: xref:System.Windows.Media.Brush objects can be declared as resources, shared among multiple objects, and cloned. In addition, all the xref:System.Windows.Media.Brush types except xref:System.Windows.Media.VisualBrush can be made read-only to improve performance and made thread-safe.
For more information about the different features provided by xref:System.Windows.Freezable objects, see Freezable Objects Overview.
For more information on why xref:System.Windows.Media.VisualBrush objects cannot be frozen, see the xref:System.Windows.Media.VisualBrush type page.
