如何使用图像绘制区域wpf
2015-07-25 23:08
465 查看
此示例演示如何使用 ImageBrush 类来绘制带有图像的区域。ImageBrush 显示由其 ImageSource 属性指定的单个图像。
源于msdn
https://msdn.microsoft.com/zh-cn/library/vstudio/ms744759(v=vs.90).aspx
下面的示例通过使用 ImageBrush 绘制按钮的 Background。
默认情况下,ImageBrush 拉伸其图像以完全填充您绘制的区域。在上面的示例中,拉伸图像以填充按钮,可能扭曲图像。可以通过将 TileBrush 的 Stretch 属性设置为 Uniform 或 UniformToFill 来控制此行为,这样会使画笔保留图像的纵横比。
如果设置 ImageBrush 的 Viewport 和 TileMode 属性,则可以创建重复的图案。下面的示例通过使用从图像创建的图案来绘制按钮。
源于msdn
https://msdn.microsoft.com/zh-cn/library/vstudio/ms744759(v=vs.90).aspx
下面的示例通过使用 ImageBrush 绘制按钮的 Background。
[code]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; } } }
[code]<!-- 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 属性,则可以创建重复的图案。下面的示例通过使用从图像创建的图案来绘制按钮。
[code]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; } } }
[code]<!-- 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>
相关文章推荐
- hdu杭电3788 ZOJ问题
- leetcode-202-Happy Number
- POJ1159 Palindrome
- Cinder Core检视规律
- [C Language] 回调函数
- hadoop集群中的日志文件
- socket编程——介绍CAsyncSocket类的用法
- hdu杭电 2147 kiki's game
- DHCP:动态主机配置协议
- Android中popupWindow的简单应用
- centos最小化安装后再安装图形界面
- Codeforces Round #312 (Div. 2)小结
- 消除 activity 启动时白屏、黑屏问题
- Centos7安装gitlab
- 基于MFC的socket编程(异步非阻塞通信)
- [C++学习笔记]--位运算、引用、const指针
- CNN卷积神经网络学习笔记4:代码学习
- (详细图解)把SQLServer数据导入Access中
- 最简单的基于FFmpeg的移动端例子附件:SDL Android HelloWorld
- asp.net重写服务器控件