如何使用圖像繪制區域wpf


此示例演示如何使用 ImageBrush 類來繪制帶有圖像的區域。ImageBrush 顯示由其 ImageSource 屬性指定的單個圖像。
源於msdn
https://msdn.microsoft.com/zh-cn/library/vstudio/ms744759(v=vs.90).aspx
下面的示例通過使用 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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://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>

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com