You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF自定义绘制大数据集时平移缩放性能极低问题求助

WPF高性能2D绘图应用性能问题

我正在用C# WPF开发一款高性能2D绘图应用,需要实现以下功能:

  • 网格(坐标轴+网格线)
  • 数千个简单对象(机场:小圆形/点)
  • 交互式平移(鼠标拖拽)和缩放(鼠标滚轮)

相同的应用逻辑在早年的其他框架中实现时性能表现极佳:

  • WinForms(GDI+)→ 速度极快
  • Qt(QPainter/QGraphicsView)→ 速度极快

但在WPF中,即使是Release模式下性能也显著下降。相关代码如下:

public class AirportMapElement : AxisVisualElement
{ 
    public Dictionary<int, List<AirportMapData>> Data { get; set; } = new();

    public double MarginLeft { get; set; } = 70;
    public double MarginRight { get; set; } = 20;
    public double MarginTop { get; set; } = 20;
    public double MarginBottom { get; set; } = 45;

    protected override Size MeasureOverride(Size availableSize)
        => availableSize;

    protected override Size ArrangeOverride(Size finalSize)
        => finalSize;

    protected override void OnRenderElement(DrawingContext dc)
    {
        if (XAxis == null || YAxis == null) return;
        if (ActualWidth <= 0 || ActualHeight <= 0) return;
        if (Data.Count == 0) return;

        var plotRect = new Rect(
            MarginLeft,
            MarginTop,
            ActualWidth - MarginLeft - MarginRight,
            ActualHeight - MarginTop - MarginBottom);

        if (plotRect.Width <= 1 || plotRect.Height <= 1) return;

        XAxis.SetViewPort(plotRect);
        YAxis.SetViewPort(plotRect);

        dc.PushClip(new RectangleGeometry(plotRect));

        var pen = new Pen(Stroke, StrokeThickness);
        if (pen.CanFreeze) pen.Freeze();

        foreach (var kv in Data)
        {
            var list = kv.Value;
            if (list == null || list.Count < 2) continue;

            double px = XAxis.CoordToPixel(list[0].fLongitude / 3600.0);
            double py = YAxis.CoordToPixel(list[0].fLatitude / 3600.0);

            for (int i = 1; i < list.Count; i++)
            {
                var p = list[i];

                double x = XAxis.CoordToPixel(p.fLongitude / 3600.0);
                double y = YAxis.CoordToPixel(p.fLatitude / 3600.0);

                dc.DrawLine(pen, new Point(px, py), new Point(x, y));

                px = x;
                py = y;
            }
        }

        dc.Pop();
    }
}

内容的提问来源于stack exchange,提问作者Akmal Bukhariev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 18:05:03