ImageBrush ImageBrush ImageBrush ImageBrush Class

Definition

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

public : sealed class ImageBrush : TileBrush, IImageBrush
struct winrt::Windows::UI::Xaml::Media::ImageBrush : TileBrush, IImageBrush
public sealed class ImageBrush : TileBrush, IImageBrush
Public NotInheritable Class ImageBrush Inherits TileBrush Implements IImageBrush
<ImageBrush .../>
Inheritance
ImageBrushImageBrushImageBrushImageBrush
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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.

<!-- TextBlock with an image brush applied to the text. -->
<TextBlock FontFamily="Verdana" FontSize="72"
 FontStyle="Italic" FontWeight="Bold">
    SHRUBBERY
  <TextBlock.Foreground>
    <ImageBrush ImageSource="forest.jpeg"/>
  </TextBlock.Foreground>
</TextBlock>

An ImageBrush applied to text

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.

Windows 8 behavior

Windows 8 had an issue with ImageSource resolution from a XAML attribute URI value, if the ImageBrush is part of a XAML style or template that's applied to a control. The control usage would sometimes use a component-specific base URI rather than the appropriate base URI for the style or template, which usually comes from the app. The issue is fixed starting with Windows 8.1; the base URI is correctly determined for app resources or component resources depending on the scope that needs the base URI. Apps that were compiled for Windows 8 might have used workarounds for this behavior, by putting their image source files in the "wrong" place, where the XAML URI would resolve and the app would show the images. If you are migrating XAML from Windows 8 to Windows 8.1 you should test any ImageBrush usages in a style or template from your XAML and verify that the image resolution in your app is working with Windows 8.1. If you have a problem, you should move your image source files within the package so that they're in the correct resource scope for the new behavior.

Apps that were compiled for Windows 8 but running on Windows 8.1 continue to use the Windows 8 behavior.

Constructors

ImageBrush() ImageBrush() ImageBrush() ImageBrush()

Initializes a new instance of the ImageBrush class.

Properties

AlignmentX AlignmentX AlignmentX AlignmentX

Gets or sets the horizontal alignment of content in the TileBrush base tile.

(Inherited from TileBrush)
AlignmentXProperty AlignmentXProperty AlignmentXProperty AlignmentXProperty

Identifies the AlignmentX dependency property.

(Inherited from TileBrush)
AlignmentY AlignmentY AlignmentY AlignmentY

Gets or sets the vertical alignment of content in the TileBrush base tile.

(Inherited from TileBrush)
AlignmentYProperty AlignmentYProperty AlignmentYProperty AlignmentYProperty

Identifies the AlignmentY dependency property.

(Inherited from TileBrush)
Dispatcher Dispatcher Dispatcher Dispatcher

Gets the CoreDispatcher that this object is associated with. The CoreDispatcher represents a facility that can access the DependencyObject on the UI thread even if the code is initiated by a non-UI thread.

(Inherited from DependencyObject)
ImageSource ImageSource ImageSource ImageSource

Gets or sets the image source displayed by this ImageBrush. In code you set this with an ImageSource subclass instance, in XAML you set this with a URI to an image source file.

ImageSourceProperty ImageSourceProperty ImageSourceProperty ImageSourceProperty

Identifies the ImageSource dependency property.

Opacity Opacity Opacity Opacity

Gets or sets the degree of opacity of a Brush.

(Inherited from Brush)
OpacityProperty OpacityProperty OpacityProperty OpacityProperty

Identifies the Opacity dependency property.

(Inherited from Brush)
RelativeTransform RelativeTransform RelativeTransform RelativeTransform

Gets or sets the transformation that is applied to the brush using relative coordinates.

(Inherited from Brush)
RelativeTransformProperty RelativeTransformProperty RelativeTransformProperty RelativeTransformProperty

Identifies the RelativeTransform dependency property.

(Inherited from Brush)
Stretch Stretch Stretch Stretch

Gets or sets a value that specifies how the content of this TileBrush stretches to fit its tiles.

(Inherited from TileBrush)
StretchProperty StretchProperty StretchProperty StretchProperty

Identifies the Stretch dependency property.

(Inherited from TileBrush)
Transform Transform Transform Transform

Gets or sets the transformation that is applied to the brush.

(Inherited from Brush)
TransformProperty TransformProperty TransformProperty TransformProperty

Identifies the Transform dependency property.

(Inherited from Brush)

Events

ImageFailed ImageFailed ImageFailed ImageFailed

Occurs when there is an error associated with image retrieval or format.

ImageOpened ImageOpened ImageOpened ImageOpened

Occurs when the image source is downloaded and decoded with no failure. You can use this event to determine the size of an image before rendering it.

Methods

ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty) ClearValue(DependencyProperty)

Clears the local value of a dependency property.

(Inherited from DependencyObject)
GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty) GetAnimationBaseValue(DependencyProperty)

Returns any base value established for a dependency property, which would apply in cases where an animation is not active.

(Inherited from DependencyObject)
GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty) GetValue(DependencyProperty)

Returns the current effective value of a dependency property from a DependencyObject.

(Inherited from DependencyObject)
ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty) ReadLocalValue(DependencyProperty)

Returns the local value of a dependency property, if a local value is set.

(Inherited from DependencyObject)
RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback) RegisterPropertyChangedCallback(DependencyProperty,DependencyPropertyChangedCallback)

Registers a notification function for listening to changes to a specific DependencyProperty on this DependencyObject instance.

(Inherited from DependencyObject)
SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object) SetValue(DependencyProperty,Object)

Sets the local value of a dependency property on a DependencyObject.

(Inherited from DependencyObject)
UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64) UnregisterPropertyChangedCallback(DependencyProperty,Int64)

Cancels a change notification that was previously registered by calling RegisterPropertyChangedCallback.

(Inherited from DependencyObject)

See Also