重新调整WPF几何路径的大小

时间:2015-09-02 19:21:19

标签: wpf geometry-path

我正在研究WPF应用程序。给定几何字符串路径,例如:

F1 M 27,18L 23,26L 33,30L 24,38L 33,46L 23,50L 27,58L 45,58L 55,38L 45,18L 27,18 Z

是否可以将图形缩放到宽度和高度(无论原件有多小/大),同时保持图形整体,然后最终返回新缩放图形的字符串路径表示?

2 个答案:

答案 0 :(得分:2)

无需缩放路径几何字符串中的值。只需将其放在Data控件的Path属性中,并根据需要设置其WidthHeightStretch属性:

<Path Data="F1 M27,18 L23,26 33,30 24,38 33,46 23,50 27,58 45,58 55,38 45,18 27,18 Z"
      Width="100" Height="100" Stretch="Uniform" Fill="Black"/>

答案 1 :(得分:0)

是的,你可以做到!您唯一需要做的就是使用Viewbox来包装物品。这是一个包含我已经完成的代码的示例,在这种情况下,我将几何用作DrawingBrush

...
<UserControl.Resources>
    <DrawingBrush x:Key="Field" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="{StaticResource FieldGrassBrush}" Geometry="F1 M 91.733,119.946C 92.8352,122.738 93.9374,125.529 92.9241,129.209C 91.9107,132.889 88.7819,137.458 84.4263,139.271C 80.0707,141.084 74.4885,140.142 70.8885,137.982C 67.2885,135.822 65.6707,132.444 65.1819,129.182C 64.693,125.92 65.333,122.773 65.973,119.626L 0.16,53.9203C 0.444319,53.4758 0.728638,53.0312 3.48413,48.7023C 6.23962,44.3733 11.4663,36.16 18.5596,28C 25.653,19.84 34.613,11.7333 
                ........
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</UserControl.Resources>
...

然后是视图框(请注意,网格已固定HeightWidth,但它会被拉伸到Viewbox的大小,在这种情况下,以统一的方式):

...
<Viewbox Stretch="Uniform" Grid.Row="2" Grid.ColumnSpan="2">
    <Grid Height="300" Width="390">
        <Rectangle Fill="{DynamicResource Field}" StrokeThickness="0"/>
...