업데이트: 2007년 11월
이 예제에서는 ImageBrush 클래스를 사용하여 이미지로 영역을 그리는 방법을 보여 줍니다. ImageBrush는 해당 ImageSource 속성에 지정된 단일 이미지를 표시합니다.
예제
다음 예제에서는 ImageBrush를 사용하여 단추의 Background를 그립니다.
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>
기본적으로 ImageBrush는 현재 그리고 있는 영역을 완전히 채우도록 이미지를 늘립니다. 앞의 예제에서는 단추를 채우기 위해 이미지가 늘어나는데 이 경우 이미지가 왜곡될 수 있습니다. TileBrush의 Stretch 속성을 Uniform 또는 UniformToFill로 설정하여 브러시가 이미지의 가로 세로 비율을 유지하도록 하면 이 동작을 제어할 수 있습니다.
ImageBrush의 Viewport 및 TileMode 속성을 설정하면 반복되는 패턴을 만들 수 있습니다. 다음 예제에서는 이미지에서 만들어진 패턴을 사용하여 단추를 그립니다.
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>
ImageBrush 클래스에 대한 자세한 내용은 이미지, 그림 및 시각적 표시로 그리기를 참조하십시오.
이 코드 예제는 ImageBrush 클래스에 대해 제공되는 보다 큰 예제의 일부입니다. 전체 샘플을 보려면 ImageBrush 샘플을 참조하십시오.