who_lee_oh
who_lee_oh

Reputation: 119

Re-sizing WPF Geometry Path

I'm working on a WPF application. Given a geometry string path, such as:

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

Is it possible to scale the drawing to a width and height (no matter how small/large the original was) while keeping the figure as a whole, and then finally return the string path representation of the new scaled figure?

Upvotes: 1

Views: 567

Answers (2)

Clemens
Clemens

Reputation: 128136

There is no need to scale the values in a path geometry string. Just put it in the Data property of a Path control and set its Width, Height and Stretch properties as needed:

<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"/>

Upvotes: 2

Ra&#250;l Ota&#241;o
Ra&#250;l Ota&#241;o

Reputation: 4770

Yes you can do it! The only thing you need to do, is to use a Viewbox for wrapping the item. This is a sample with a code that I had done, in this case I used the geometry as a 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>
...

Then the View Box (Note that the Grid has fixed Height and Width, but it will be stretched to the Viewbox's size, in this case, in an uniform way):

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

Upvotes: 0

Related Questions