WPF 用 DrawingBrush实现图形的平铺
WPF平铺图像,一般用到 DrawingBrush 来实现对图形、图形的平铺,查阅了 微软官方文档的 DrawingBrush 属性
创建一个 DrawingBrush,其中:
TileMode="Tile"设置了改画刷为平铺的属性
1 <DrawingBrush x:Key="test" Stretch="None" Viewport="0,0,78,78" ViewportUnits="Absolute" TileMode="Tile"> 2 <DrawingBrush.Drawing> 3 <DrawingGroup> 4 <DrawingGroup.Children> 5 <GeometryDrawing> 6 <GeometryDrawing.Geometry> 7 <RectangleGeometry Rect="0,0,78,78" /> 8 </GeometryDrawing.Geometry> 9 <GeometryDrawing.Pen> 10 <Pen Brush="{StaticResource GridLineBrush}" Thickness="3"/> 11 </GeometryDrawing.Pen> 12 </GeometryDrawing> 13 <GeometryDrawing> 14 <GeometryDrawing.Geometry> 15 <GeometryGroup> 16 <LineGeometry StartPoint="0.5,19.5" EndPoint="77.5,19.5" /> 17 <LineGeometry StartPoint="0.5,39" EndPoint="77.5,39" /> 18 <LineGeometry StartPoint="0.5,58.5" EndPoint="77.5,58.5" /> 19 <LineGeometry StartPoint="19.5,0.5" EndPoint="19.5,77.5" /> 20 <LineGeometry StartPoint="39,0.5" EndPoint="39,77.5" /> 21 <LineGeometry StartPoint="58.5,0.5" EndPoint="58.5,77.5" /> 22 </GeometryGroup> 23 </GeometryDrawing.Geometry> 24 <GeometryDrawing.Pen> 25 <Pen Brush="{StaticResource GridLineBrush}" Thickness="1"/> 26 </GeometryDrawing.Pen> 27 </GeometryDrawing> 28 </DrawingGroup.Children> 29 </DrawingGroup> 30 </DrawingBrush.Drawing> 31 </DrawingBrush>
画刷会自动适应平铺到窗体一样的大小,效果图如下: