WPF:理解TileBrush(ImageBrush,DrawingBrush和VisualBrush) .

先来看看ImageBrush在不使用铺设方式时的效果:
ImageBrush不使用铺设方式时的效果
图1为原图,图2,3,4为使用ImageBrush填充到椭圆中的效果。
图1的XAML代码:
  

1 <Border Width="142" BorderBrush="#FF000000" BorderThickness="1,1,1,1" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Margin="8,11.159,0,0" VerticalAlignment="Top" Height="150">
2    <Image Source="xian.png" Stretch="Fill" Width="142" Height="150"/>
3   </Border>


(Border作为定位及画边框用途)

图2的XAML代码:

1 <Ellipse x:Name="ellipseWithImageBrush" Stroke="#FF000000" Height="150" Margin="169.557,8,0,0" HorizontalAlignment="Left" Width="150" d:LayoutOverrides="Height" VerticalAlignment="Top">
2    <Ellipse.Fill>
3     <ImageBrush ImageSource="xian.png"/>
4    </Ellipse.Fill>
5   </Ellipse>


这里使用了Ellipse的Fill属性,这样ImageBrush就作为Ellipse的填充画刷了。

图3的XAML代码:

1 <Ellipse x:Name="ellipsFillWithImageBrush" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Margin="0,11.159,167.443,0" VerticalAlignment="Top" Height="150" HorizontalAlignment="Right" Width="150">
2   <Ellipse.Fill>
3  <ImageBrush ImageSource="xian.png" Viewport="-0.05,-0.1,1,1.1" />
4   </Ellipse.Fill>
5 </Ellipse>

图4的XAML代码:

1 <Ellipse x:Name="ellipsFillWithImageBrushFill" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Margin="0,8,2,0" VerticalAlignment="Top" Height="150" HorizontalAlignment="Right" Width="150">
2   <Ellipse.Fill>
3     <ImageBrush ImageSource="xian.png" Viewport="-0.16,-0.16,1.18,1.28"/>
4   </Ellipse.Fill>
5 </Ellipse>


你可以自行比较Viewport参数对最终效果产生的影响。(关于Viewport的更多说明见后)

再来看看ImageBrush的各种铺设方式效果:
ImageBrush的铺设方式

下面分别予以介绍各种不同的辅设方式及其代码。

上图5至图9中,我们在ImageBrush中都使用了Viewport="0,0,0.3,0.3"。其中,前两个参数(0,0)表示起始位置,后面两个参数(0.3,0.3)表示缩放比例。例如图5(XAML代码):

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile" Stroke="#FF000000" HorizontalAlignment="Left" Margin="8,177.015,0,240.985" Width="208" Height="208">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      Viewport="0,0,0.3,0.3"
 5      TileMode="None"
 6      AlignmentX="Left"
 7      AlignmentY="Top"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


(由于使用了TileMode.None,所以,圆形中小图没有平铺,只在左上角放了一张小图片)

图6的XAML代码:

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile_Copy3" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Height="208" d:LayoutOverrides="Height" Margin="225,177.015,219,240.985">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      TileMode="FlipX"
 5      AlignmentX="Left"
 6      AlignmentY="Top"
 7      Viewport="0,0,0.3,0.3"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


说明:由于这里使用了TileMode.FlipX,我们发现图片在水平(X)方向上做了镜像对称反转。

图7的XAML代码:
  

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile_Copy1" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Margin="0,177.015,4,240.985" d:LayoutOverrides="Width" HorizontalAlignment="Right" Width="208" Height="208">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      Viewport="0,0,0.3,0.3"
 5      TileMode="FlipY"
 6      AlignmentX="Left"
 7      AlignmentY="Top"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


说明:由于这里使用了TileMode.FlipY,我们发现图片在垂直(Y)方向上做了镜像对称反转(正立/倒立)。

图8的XAML代码:
  

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile_Copy4" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Width="208" Height="208" d:LayoutOverrides="Width, Height" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      Viewport="0,0,0.3,0.3"
 5      TileMode="Tile"
 6      AlignmentX="Left"
 7      AlignmentY="Top"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


说明:由于这里使用了TileMode.Tile,我们发现图片象铺地砖式地一张张平铺到一起,填充到圆中,但没有做任何镜像对称反转。

图9的XAML代码:
  

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile_Copy2" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Margin="225,0,219,8" d:LayoutOverrides="Height" VerticalAlignment="Bottom" Height="208">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      Viewport="0,0,0.3,0.3"
 5      TileMode="FlipXY"
 6      AlignmentX="Left"
 7      AlignmentY="Top"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


说明:由于这里使用了TileMode.FlipXY,我们发现图片不但在水平方向上做了镜像对称反转,而且在垂直(Y)方向上做了镜像对称反转(正立/倒立)。

图10的XAML代码:
  

 1 <Ellipse x:Name="ellipsFillWithImageBrushTile_Copy" Stroke="#FF000000" RenderTransformOrigin="0.5,0.5" Height="208" d:LayoutOverrides="Height" HorizontalAlignment="Right" Margin="0,0,4,8" VerticalAlignment="Bottom" Width="208">
 2    <Ellipse.Fill>
 3     <ImageBrush ImageSource="xian.png"
 4      Viewport="0,0,0.18,0.18"
 5      TileMode="FlipXY"
 6      AlignmentX="Left"
 7      AlignmentY="Top"
 8     />
 9    </Ellipse.Fill>
10   </Ellipse>


比较图9和图10,看看它们的代码,Viewport属性值由"0,0,0.3,0.3"变成了"0,0,0.18,0.18",这样,我们看到的里面填充的小图片比例缩得更小了(当然也就可以放更多的小图片了)。

需要指出的是,Viewport是在WPF中,使用一个Rect对象来表示。如果需要指定绝对数值而不是缩放比例,那么,就需要设置BrushMappingMode为BrushMappingMode.Absolute。同时指定Viewport的绝对值,比如将图6所对应的XAML代码改为:

1 <ImageBrush
2       Viewport="0,0,25,25"
3       ViewportUnits="Absolute"
4       TileMode="Tile"
5       ImageSource="xian.png" />



之后,得到如下效果:
使用Viewport绝对值的效果
这里的填充小图每个图的宽度和高度分别为25,25像素了。

为了加深大家的印象,不妨与GDI+的图片画刷作一个比较。
在GDI+中,与WPF中ImageBrush对应的是TextureBrush,与ImageBrush.TileMode对应的是TextureBrush.WrapMode, 而WrapMode的枚举值(注意是“枚举值”)是:Clamp,Tile,

TileFlipX,TileFlipY和TileFlipXY,分别与TileMode属性值(注意是“属性值”)的None,Tile,FlipX,FlipY和FlipXY对应。
与WPF中指定Viewport属性来控制起点及图像缩放不同的是:在GDI+中,graphic.RenderingOrigin将对TextureBrush的图片绘制的起始位置产生影响,如果需要对TextureBrush 对象的局部做几何变换缩放,可以使用ScaleTransform()方法,例如:

1 public void ScaleTransform_Example(PaintEventArgs e)
2 {
3   TextureBrush tBrush = new TextureBrush(new Bitmap("texture.jpg"));
4   tBrush.ScaleTransform(2, 1, MatrixOrder.Prepend);
5   e.Graphics.FillRectangle(tBrush, 0, 0, 100, 100);
6 }

转载:http://blog.csdn.net/johnsuna/article/details/1772969

原文地址:https://www.cnblogs.com/duqingsong/p/3092411.html