首页 > 解决方案 > 在类似于 GDI - Arc() 方法的 WPF 应用程序中绘制 Arc 很热?

问题描述

我目前正在将 C++ 应用程序移植到 WPF。C++ 应用程序使用 GDI 进行图形绘制。为了绘制 Arc,在 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);
    }

标签: c#wpfgdi

解决方案


推荐阅读