방법: 이미지로 영역 그리기
이 예제에서는 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;
}
}
}
기본적으로 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;
}
}
}
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
ImageBrush 클래스에 대한 자세한 내용은 이미지, 그림 및 시각적 표시로 그리기를 참조하세요.
이 코드 예제는 제공 된 큰 예제의 일부는 ImageBrush 클래스입니다. 전체 샘플을 보려면 ImageBrush 샘플을 참조하세요.
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET Desktop feedback