热在WPF应用程序中绘制与GDI类似的Arc-Arc()方法?

时间:2019-06-18 05:59:01

标签: c# wpf gdi

我目前正在将C ++应用程序移植到WPF。 C ++应用程序使用GDI进行图形绘制。 为了绘制弧线,在C ++应用程序中使用了以下GDI API。

WINAPI Arc( __in HDC hdc, __in int x1, __in int y1, __in int x2, __in int y2, __in int x3, __in int y3, __in int x4, __in int y4);

我需要将其转换为WPF Arc。请帮助

我已经尝试过在WPF中绘制弧段。但是输出与GDI图不匹配。

    private void DrawArcSegment(double strokeThickness, SolidColorBrush colorBrush, 
        double left, double top, double right, double bottom,
        double startArcX, double startArcY, double endArcX, double endArcY)
    {
        System.Windows.Shapes.Path arc = new System.Windows.Shapes.Path();
        PathFigure figure = new PathFigure();
        PathSegmentCollection segments = new PathSegmentCollection();

        Point center = new Point(left + ((right - left) / 2), top + ((bottom - top) / 2));
        double degrees = Math.Atan2(startArcY - center.Y, startArcX - center.X)
                        - Math.Atan2(endArcY - center.Y, endArcX - center.X);

        degrees *= 57.2957795; // Convert from radians to degrees
        bool isLargeArc = Math.Abs(degrees) > 180;

        arc.Data = new PathGeometry();

        figure.StartPoint = new Point((startArcX), (startArcY));

        ArcSegment segment = new ArcSegment
        {
            Point = new Point(endArcX, endArcY),
            Size = new Size((right - left) / 2, (bottom - top) / 2),
            RotationAngle = 0,
            IsLargeArc = isLargeArc,
            SweepDirection = SweepDirection.Counterclockwise
        };

        segments.Add(segment);

        figure.Segments = segments;
        arc.Stroke = colorBrush;
        arc.StrokeThickness = strokeThickness;
        ((PathGeometry)arc.Data).Figures.Add(figure);

        drawingCanvas.Children.Add(arc);
    }

0 个答案:

没有答案