【问题标题】:Trying to learn DrawingVisual and StreamGeometry尝试学习 DrawingVisual 和 StreamGeometry
【发布时间】:2015-11-12 14:08:01
【问题描述】:

在我的应用程序中,我必须绘制一条包含很多点的线(如果需要,最多可达 100.000)。最初我使用折线,但结果对于大量点来说太慢了。

我一直在做一些阅读并来到 DrawingVisual 和 StreamGeometry,但是它不起作用。

我按照一个简单的教程创建了以下课程:

class ConcentratieGrafiek : FrameworkElement
{
    VisualCollection Visuals;
    PointCollection PuntenCollectie;

    public ConcentratieGrafiek(PointCollection Punten)
    {
        Visuals = new VisualCollection(this);
        PuntenCollectie = Punten;

        this.Loaded += new RoutedEventHandler(ConcentratieGrafiekLoaded);
    }

    void ConcentratieGrafiekLoaded(object sender, RoutedEventArgs e)
    {
        DrawingVisual Visual = new DrawingVisual();

        StreamGeometry g = new StreamGeometry();
        using (StreamGeometryContext cr = g.Open())
        {
            foreach (Point Punt in PuntenCollectie)
            {
                if (PuntenCollectie.IndexOf(Punt).Equals(0))
                {
                    cr.BeginFigure(Punt, false, false);
                }
                else
                {
                    cr.LineTo(Punt, false, false);
                }
            }
        }

        using (DrawingContext dc = Visual.RenderOpen())
        {
            Pen p = new Pen(Brushes.Black, 1);
            dc.DrawGeometry(Brushes.Black, p, g);
        }

        Visuals.Add(Visual);
    }

    protected override Visual GetVisualChild(int index)
    {
        return Visuals[index];
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return Visuals.Count;
        }
    }
}

我需要的线的所有点都在名为 Punten 的 PointCollection 中。然后我尝试将此 FrameworkElement 添加到我的画布中。

ActieveCanvas.Children.Add(new ConcentratieGrafiek(ConcentratiePunten) { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch });

其中“ActieveCanvas”是画布。

供参考:所有点都是画布内(相对于)的 X、Y 坐标。

我做错了什么?

【问题讨论】:

    标签: c# wpf canvas drawing drawingvisual


    【解决方案1】:

    通过更改以下内容来修复它

                if (PuntenCollectie.IndexOf(Punt).Equals(0))
                {
                    cr.BeginFigure(Punt, false, false);
                }
                else
                {
                    cr.LineTo(Punt, false, false);
                }
    

                if (PuntenCollectie.IndexOf(Punt).Equals(0))
                {
                    cr.BeginFigure(Punt, true, false);
                }
                else
                {
                    cr.LineTo(Punt, true, false);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多