Cómo: Pintar un área con una imagen

En este ejemplo se muestra cómo usar la clase ImageBrush para pintar un área con una imagen. Un elemento ImageBrush muestra una única imagen, que se especifica mediante su propiedad ImageSource.

Ejemplo

En el ejemplo siguiente se pinta el elemento Background de un botón mediante ImageBrush.


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Microsoft.Samples.Graphics.UsingImageBrush
{

    public class PaintingWithImagesExample : Page
    {

        public PaintingWithImagesExample()
        {
            Background = Brushes.White;
            StackPanel mainPanel = new StackPanel();
            mainPanel.Margin = new Thickness(20.0);

            // Create a button.
            Button berriesButton = new Button();
            berriesButton.Foreground = Brushes.White;
            berriesButton.FontWeight = FontWeights.Bold;
            FontSizeConverter sizeConverter = new FontSizeConverter();
            berriesButton.FontSize = (Double)sizeConverter.ConvertFromString("16pt");
            berriesButton.FontFamily = new FontFamily("Verdana");
            berriesButton.Content = "Berries";
            berriesButton.Padding = new Thickness(20.0);
            berriesButton.HorizontalAlignment = HorizontalAlignment.Left;

            // Create an ImageBrush.
            ImageBrush berriesBrush = new ImageBrush();
            berriesBrush.ImageSource =
                new BitmapImage(
                    new Uri(@"sampleImages\berries.jpg", UriKind.Relative)
                );

            // Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush;

            mainPanel.Children.Add(berriesButton);
            this.Content = mainPanel;
        }
    }
}

De manera predeterminada, un objeto ImageBrush ajusta su imagen hasta rellenar completamente el área que se está pintando. En el ejemplo anterior, la imagen se ajusta para rellenar el botón, lo que puede distorsionar la imagen. Puede controlar este comportamiento estableciendo la propiedad Stretch de TileBrush en Uniform o UniformToFill, lo que hace que el pincel conserve la relación de aspecto de la imagen.

Si establece las propiedades Viewport y TileMode de un elemento ImageBrush, puede crear un patrón que se repite. En el ejemplo siguiente se pinta un botón mediante un patrón creado a partir de una imagen.


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Microsoft.Samples.Graphics.UsingImageBrush
{

    public class TiledImageBrushExample : Page
    {

        public TiledImageBrushExample()
        {
            Background = Brushes.White;
            StackPanel mainPanel = new StackPanel();
            mainPanel.Margin = new Thickness(20.0);

            // Create a button.
            Button berriesButton = new Button();
            berriesButton.Foreground = Brushes.White;
            berriesButton.FontWeight = FontWeights.Bold;
            FontSizeConverter sizeConverter = new FontSizeConverter();
            berriesButton.FontSize = (Double)sizeConverter.ConvertFromString("16pt");
            berriesButton.FontFamily = new FontFamily("Verdana");
            berriesButton.Content = "Berries";
            berriesButton.Padding = new Thickness(20.0);
            berriesButton.HorizontalAlignment = HorizontalAlignment.Left;

            // Create an ImageBrush.
            ImageBrush berriesBrush = new ImageBrush();
            berriesBrush.ImageSource =
                new BitmapImage(
                    new Uri(@"sampleImages\berries.jpg", UriKind.Relative)
                );

            // Set the ImageBrush's Viewport and TileMode
            // so that it produces a pattern from
            // the image.
            berriesBrush.Viewport = new Rect(0,0,0.5,0.5);
            berriesBrush.TileMode = TileMode.FlipXY;

            // Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush;

            mainPanel.Children.Add(berriesButton);
            this.Content = mainPanel;
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports System.Windows.Media

Namespace Microsoft.Samples.Graphics.UsingImageBrush

    Public Class TiledImageBrushExample
        Inherits Page

        Public Sub New()
            Background = Brushes.White
            Dim mainPanel As New StackPanel()
            mainPanel.Margin = New Thickness(20.0)

            ' Create a button.
            Dim berriesButton As New Button()
            With berriesButton
                .Foreground = Brushes.White
                .FontWeight = FontWeights.Bold
                Dim sizeConverter As New FontSizeConverter()
                .FontSize = CType(sizeConverter.ConvertFromString("16pt"), Double)
                .FontFamily = New FontFamily("Verdana")
                .Content = "Berries"
                .Padding = New Thickness(20.0)
                .HorizontalAlignment = HorizontalAlignment.Left
            End With

            ' Create an ImageBrush.
            Dim berriesBrush As New ImageBrush()
            berriesBrush.ImageSource = New BitmapImage(New Uri("sampleImages\berries.jpg", UriKind.Relative))

            ' Set the ImageBrush's Viewport and TileMode
            ' so that it produces a pattern from
            ' the image.
            berriesBrush.Viewport = New Rect(0, 0, 0.5, 0.5)
            berriesBrush.TileMode = TileMode.FlipXY

            ' Use the brush to paint the button's background.
            berriesButton.Background = berriesBrush

            mainPanel.Children.Add(berriesButton)
            Me.Content = mainPanel
        End Sub
    End Class
End Namespace

Para obtener más información sobre la clase ImageBrush, consulte Pintar con imágenes, dibujos y elementos visuales.

Este ejemplo de código es parte de un ejemplo más grande proporcionado para la clase ImageBrush. Para ver el ejemplo completo, consulte Ejemplo de ImageBrush.

Vea también