Skip to content

Latest commit

 

History

History
52 lines (32 loc) · 4.71 KB

File metadata and controls

52 lines (32 loc) · 4.71 KB
-api-id -api-type
T:Microsoft.UI.Xaml.Media.ImageBrush
winrt class

Microsoft.UI.Xaml.Media.ImageBrush

-description

Paints an area with an image. The image source is typically obtained from file formats such as Joint Photographic Experts Group (JPEG).

-xaml-syntax

<ImageBrush .../>

-remarks

An ImageBrush is a type of brush that defines its content as an image which can be optionally stretched and aligned. Uses for an ImageBrush include decorative effects for text, or image backgrounds for controls or layout containers.

It's useful to use an ImageBrush instead of an Image control in two main scenarios:

  1. You want to paint a non-rectangular area such as an ellipse or border with an image
  2. You want to use a single ImageBrush to paint multiple areas or UIElements with the same image, which is more efficient than using multiple Image controls

If you define an ImageBrush using code, use the default constructor, then set ImageBrush.ImageSource. This requires a BitmapImage (not a Uniform Resource Identifier (URI)) in code. If your source is a stream, use the SetSourceAsync method to initialize the value. If your source is a Uniform Resource Identifier (URI), which includes content in your app that uses the ms-appx or ms-resource schemes, use the BitmapImage constructor that takes a Uniform Resource Identifier (URI). You might also consider handling the 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. See XAML images sample for example code.

Note

You can use automatic handling for accessing unqualified resources with current scale and culture qualifiers, or you can use ResourceManager and ResourceMap with qualifiers for culture and scale to obtain the resources directly. For more info see Resource management system.

The Stretch property is important for how the image is applied when used as a brush. Some images look good when stretched as applied to a particular Brush property with the Fill behavior whereas other images do not stretch or scale well and might require a value of None or Uniform for Stretch. Experiment with different values for Stretch to see which behavior looks best when applied to the UI.

Image sources and scaling

You should create your image sources at several recommended sizes, to ensure that your app looks great when Windows 8 scales it. When specifying an ImageSource for an ImageBrush, you can use a naming convention that will automatically reference the correct resource for the current scaling. For specifics of the naming convention and more info, see Quickstart: Using file or image resources.

For more info on how to design for scaling, see UX guidelines for layout and scaling.

-examples

This XAML example shows how to set the Foreground property of a TextBlock to an ImageBrush, whose image is used as the fill for the TextBlock-rendered text.

[!code-xamlimaging_ovw_painting_with_an_image]

An ImageBrush applied to text

-see-also

TileBrush, Image, Image and ImageBrush, XAML vector-based drawing sample (see Scenario 2)