方法 : イメージで領域を塗りつぶす
更新 : 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 のサンプル」を参照してください。