Working With ArcSegment Class in WPF


Here we discuss the ArcSegment Class. It creates an arc which is basically based on the starting point of the PathFigure object. Now we take an example of the ArcSegment Class.

    <Grid>
        <Path Stroke="Green" Fill="Pink" StrokeThickness="1" Width="Auto">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigureCollection>
                            <PathFigure StartPoint="150,200">
                                <PathFigure.Segments>
                                    <PathSegmentCollection>
                                        <ArcSegment
                                                    Point="300,300"
                                                    Size="700,100"
                                                    RotationAngle="90"
                                                    IsLargeArc="False" SweepDirection="Clockwise"
                                            />
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
        </Path>
    </Grid>

The output will be:

arcsegment1.gif

Now we look at the code:

<ArcSegment Point="300,300" Size="700,100" RotationAngle="90" IsLargeArc="False" SweepDirection="Clockwise" />

Here the Point is used to define the terminal point of the Arc. The Size specifies the X and Y radius of the arc. The Rotation Angle specifies the X axis of the arc. IsLargeArc specifies that the arc will be drawn more than 180 degrees.
And SweepDirection specifies whether the arc sweeps clockwise or counterclockwise.

We can also do it with the help of coding:

In this example, we use a Button Control; when we click on the Button the Arc will appear:

<StackPanel Name="StackPanel1">
        <Button Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75"
Click="button1_Click">Click Me!!!</Button>
    </StackPanel>

Now we write the code in the .cs page:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        PathGeometry pathGeometry = new PathGeometry();
        PathFigure figure = new PathFigure();
        figure.StartPoint = new Point(150, 200);
        figure.Segments.Add(
        new ArcSegment(
        new Point(300, 200),
        new Size(700, 100),
        90,
        false,
        SweepDirection.Clockwise,
       
true
        )
        );
        pathGeometry.Figures.Add(figure);
        Path path = new Path();
        path.Data = pathGeometry;
        path.Fill = Brushes.Pink;
        path.Stroke = Brushes.Green;
   
        StackPanel1.Children.Add(path);
     }

Here we create the instance of the PathGeometry() and Path Figure Object.

The output Will Be:

arcsegment2.gif

Up Next
    Ebook Download
    View all
    Learn
    View all