Procedura: disegnare un'area con un'immagine
In questo esempio viene illustrato come utilizzare la classe ImageBrush per disegnare un'area con un'immagine. Un oggetto ImageBrush visualizza una sola immagine, specificata dalla relativa proprietà ImageSource.
Esempio
Nell'esempio riportato di seguito viene disegnata la proprietà Background di un pulsante utilizzando un oggetto ImageBrush.
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media.Imaging
Imports System.Windows.Media
Namespace Microsoft.Samples.Graphics.UsingImageBrush
Public Class PaintingWithImagesExample
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))
' 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
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;
}
}
}
<!-- This example shows how to use an ImageBrush to paint shapes and controls. -->
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Background="White">
<StackPanel Margin="20">
<!-- Sets the button's Background property with an
ImageBrush. The resulting
button has an image as its background. -->
<Button
Foreground="White" FontWeight="Bold"
FontSize="16pt" FontFamily="Verdana"
Content="Berries"
Padding="20"
HorizontalAlignment="Left">
<Button.Background>
<ImageBrush ImageSource="sampleImages\berries.jpg" />
</Button.Background>
</Button>
</StackPanel>
</Page>
Per impostazione predefinita, un oggetto ImageBrush estende la propria immagine in modo da riempire completamente l'area che si sta disegnando. Nell'esempio precedente, l'immagine viene estesa per riempire il pulsante, con il rischio che l'immagine venga distorta. È possibile controllare questo comportamento impostando la proprietà Stretch di TileBrush su Uniform o UniformToFill in modo che il pennello mantenga leproporzioni dell'immagine.
Se si impostano le proprietà Viewport e TileMode di un oggetto ImageBrush, è possibile creare un pattern ripetuto. Nell'esempio riportato di seguito viene disegnato un pulsante utilizzando un pattern creato da un'immagine.
Imports System
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
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;
}
}
}
<!-- This example shows how to use an ImageBrush to paint shapes and controls. -->
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Background="White">
<StackPanel Margin="20">
<!-- Sets the button's Background property with an
ImageBrush. The resulting
button has an image as its background. -->
<Button
Foreground="White" FontWeight="Bold"
FontSize="16pt" FontFamily="Verdana"
Content="Berries"
Padding="20"
HorizontalAlignment="Left">
<Button.Background>
<!-- The ImageBrush's Viewport and TileMode
are set to produce a pattern from the
image. -->
<ImageBrush
Viewport="0,0,0.5,0.5"
TileMode="FlipXY"
ImageSource="sampleImages\berries.jpg" />
</Button.Background>
</Button>
</StackPanel>
</Page>
Per ulteriori informazioni sulla classe ImageBrush, vedere Disegnare con oggetti Image, Drawing e Visual.
Questo esempio di codice fa parte di un esempio più esteso fornito per la classe ImageBrush. Per l'esempio completo, vedere Esempio ImageBrush (la pagina potrebbe essere in inglese).