You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/develop/platform/xaml/brushes.md
+32-33Lines changed: 32 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,16 +2,22 @@
2
2
ms.assetid: 02141F86-355E-4046-86EA-2A89D615B7DB
3
3
title: Use brushes
4
4
description: Brush objects are used to paint the interiors or outlines of shapes, text, and parts of controls, so that the object being painted is visible in a UI.
5
-
ms.date: 04/28/2020
5
+
ms.date: 10/31/2025
6
6
ms.topic: how-to
7
-
keywords: windows 10, uwp
8
7
ms.localizationpriority: medium
9
8
---
10
9
# Using brushes to paint backgrounds, foregrounds, and outlines
11
10
12
11
Use [**Brush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.Brush) objects to paint the interiors and outlines of XAML shapes, text, and controls, making them visible in your application UI.
@@ -107,20 +112,6 @@ The color of each point between gradient stops is linearly interpolated as a com
107
112
108
113
You can change the line at which the gradient stops are positioned by setting the [**StartPoint**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.lineargradientbrush.startpoint) and [**EndPoint**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.lineargradientbrush.endpoint) properties to be different values than the `(0,0)` and `(1,1)` starting defaults. By changing the **StartPoint** and **EndPoint** coordinate values, you can create horizontal or vertical gradients, reverse the gradient direction, or condense the gradient spread to apply to a smaller range than the full painted area. To condense the gradient, you set values of **StartPoint** and/or **EndPoint** to be something that is between the values 0 and 1. For example, if you want a horizontal gradient where the fade all happens on the left half of the brush and the right side is solid to your last [**GradientStop**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.GradientStop) color, you specify a **StartPoint** of `(0,0)` and an **EndPoint** of `(0.5,0)`.
109
114
110
-
### Use tools to make gradients
111
-
112
-
Now that you know how linear gradients work, you can use Visual Studio or Blend to make creating these gradients easier. To create a gradient, select the object you want to apply a gradient to on the design surface or in XAML view. Expand **Brush** and select the **Linear Gradient** tab.
113
-
114
-

115
-
116
-
*Creating a linear gradient in Visual Studio*
117
-
118
-
Now you can change the colors of the gradient stops and slide their positions using the bar on the bottom. You can also add new gradient stops by clicking on the bar and remove them by dragging the stops off of the bar (see next screenshot).
119
-
120
-

121
-
122
-
*Gradient setting slider*
123
-
124
115
## Radial gradient brushes
125
116
126
117
A [**RadialGradientBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.radialgradientbrush) is drawn within an ellipse that is defined by the [**Center**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.radialgradientbrush.center), [**RadiusX**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.radialgradientbrush.radiusx), and [**RadiusY**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.radialgradientbrush.radiusy) properties. Colors for the gradient start at the center of the ellipse and end at the radius.
@@ -187,10 +178,6 @@ You can also use brushes to apply rendering characteristics to text elements. Fo
187
178
188
179
Even when you use a solid color, make sure that the text color you choose has enough contrast against the background color of the text's layout container. The level of contrast between text foreground and text container background is an accessibility consideration.
189
180
190
-
## WebViewBrush (UWP only)
191
-
192
-
A [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush) is a special type of brush that can access the content normally viewed in a [**WebView**](/uwp/api/windows.ui.xaml.controls.webview) control. Instead of rendering the content in the rectangular **WebView** control area, **WebViewBrush** paints that content onto another element that has a [**Brush**](/uwp/api/windows.ui.xaml.Media.Brush)-type property for a render surface. **WebViewBrush** isn't appropriate for every brush scenario, but is useful for transitions of a **WebView**. For more info, see [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush).
193
-
194
181
## XamlCompositionBrushBase
195
182
196
183
[**XamlCompositionBrushBase**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.xamlcompositionbrushbase) is a base class used to create custom brushes that use [**CompositionBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.Composition.CompositionBrush) to paint XAML UI elements.
@@ -217,21 +204,33 @@ To create a [**SolidColorBrush**](/windows/windows-app-sdk/api/winrt/microsoft.u
blueBrush = ref new SolidColorBrush(Windows::UI::Colors::Blue);
230
-
```
211
+
For [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush), use the default constructor and then call other APIs before you attempt to use that brush for a UI property.
212
+
213
+
[**ImageSource**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imagebrush.imagesourceproperty) requires a [**BitmapImage**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.Imaging.BitmapImage) (not a URI) when you define an [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush) using code. If your source is a stream , use the [**SetSourceAsync**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapsource.setsourceasync) method to initialize the value. If your source is a URI, which includes content in your app that uses the **ms-appx** or **ms-resource** schemes, use the [**BitmapImage**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapimage) constructor that takes a URI. You might also consider handling the [**ImageOpened**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imagebrush.imageopened) event if there are any timing issues with retrieving or decoding the image source, where you might need alternate content to display until the image source is available.
214
+
215
+
For code examples, see [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush) and [**XamlCompositionBrushBase**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.xamlcompositionbrushbase).
216
+
217
+
## UWP and WinUI 2
231
218
232
-
For [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush) and [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush), use the default constructor and then call other APIs before you attempt to use that brush for a UI property.
Brush APIs exist in the [Windows.UI.Xaml.Controls](/uwp/api/Windows.UI.Xaml.media) and [Windows.UI.Xaml.Controls](/uwp/api/windows.ui.xaml.controls) namespaces.
> -[Open the WinUI 2 Gallery app and see RadialGradientBrush in action](winui2gallery:/item/RadialGradientBrush). [!INCLUDE [winui-2-gallery](../../../../includes/winui-2-gallery.md)]
227
+
228
+
We recommend using the latest [WinUI 2](/windows/uwp/get-started/winui2/) to get the most current styles, templates, and features for all controls.
229
+
230
+
### WebViewBrush (UWP only)
231
+
232
+
A [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush) is a special type of brush that can access the content normally viewed in a [**WebView**](/uwp/api/windows.ui.xaml.controls.webview) control. Instead of rendering the content in the rectangular **WebView** control area, **WebViewBrush** paints that content onto another element that has a [**Brush**](/uwp/api/windows.ui.xaml.Media.Brush)-type property for a render surface. **WebViewBrush** isn't appropriate for every brush scenario, but is useful for transitions of a **WebView**. For more info, see [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush).
233
233
234
-
- [**ImageSource**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imagebrush.imagesourceproperty) requires a [**BitmapImage**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.Imaging.BitmapImage) (not a URI) when you define an [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush) using code. If your source is a stream , use the [**SetSourceAsync**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapsource.setsourceasync) method to initialize the value. If your source is a URI, which includes content in your app that uses the **ms-appx** or **ms-resource** schemes, use the [**BitmapImage**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapimage) constructor that takes a URI. You might also consider handling the [**ImageOpened**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imagebrush.imageopened) event if there are any timing issues with retrieving or decoding the image source, where you might need alternate content to display until the image source is available.
235
-
- For [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush) you might need to call [**Redraw**](/uwp/api/windows.ui.xaml.controls.webviewbrush.redraw) if you've recently reset the [**SourceName**](/uwp/api/windows.ui.xaml.controls.webviewbrush.sourcename) property or if the content of the [**WebView**](/uwp/api/windows.ui.xaml.controls.webview) is also being changed with code.
234
+
If you create a [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush) in code, use the default constructor and then call other APIs before you attempt to use that brush for a UI property. You might need to call [**Redraw**](/uwp/api/windows.ui.xaml.controls.webviewbrush.redraw) if you've recently reset the [**SourceName**](/uwp/api/windows.ui.xaml.controls.webviewbrush.sourcename) property or if the content of the [**WebView**](/uwp/api/windows.ui.xaml.controls.webview) is also being changed with code.
236
235
237
-
For code examples, see [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush), [**ImageBrush**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.Media.ImageBrush), and [**XamlCompositionBrushBase**](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.xamlcompositionbrushbase).
236
+
For code examples, see [**WebViewBrush**](/uwp/api/windows.ui.xaml.controls.webviewbrush).
0 commit comments