1

I need to create a grid using Canvas with horizontal and vertical lines. The problem is in very bad performance when I'm using dashed lines instead of solid. Is there any solution to solve this? I don't need the possibility to handle events of this dashed lines (maybe exists some 'lightweight' version of canvas object...).

If I add StrokeDashArray to the Line object the application is very slow...

        private void DrawGrid()
        {
            var brush = new SolidColorBrush((Color) ColorConverter.ConvertFromString("#cccccc"));

            for (int i = 100; i < _areaSize; i += 100)
            {
                var hLine = new Line
                {
                    X1 = 0,
                    Y1 = i,
                    X2 = _areaSize,
                    Y2 = i,
                    Stroke = brush,
                    StrokeThickness = 1,
                    SnapsToDevicePixels = true,
                };

                var vLine = new Line
                {
                    X1 = i,
                    Y1 = 0,
                    X2 = i,
                    Y2 = _areaSize,
                    Stroke = brush,
                    StrokeThickness = 1,
                    SnapsToDevicePixels = true
                };

                //hLine.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
                //vLine.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

                Container.Children.Add(hLine);
                Container.Children.Add(vLine);
                Panel.SetZIndex(hLine, -1000);
                Panel.SetZIndex(vLine, -1000);
            }
        }
Alexander Hryk
  • 585
  • 2
  • 4
  • 16
  • What do you mean with "very bad performance". You create the lines once, what happens then? How large is _areaSize? How does your potential StrokeDashArray look like? – Clemens Nov 23 '17 at 17:10
  • When I'm using solid lines I can drag the canvas without any freeze, but when lines are dashed the application starts very slow and there are freezes when I'm dragging canvas. _areaSize = 100 000 (I wrapped the Canvas in ScrollViewer) – Alexander Hryk Nov 23 '17 at 17:14
  • did you try the performance profiler? you could profile the performance with dash and without dash and compare the two profiles, maybe you will find a difference. maybe the dashed line takes more processing than the solid line – Siraf Nov 23 '17 at 17:22
  • 1
    From my tests i think the problem is adding the children to the collection. Theres a lot of events on rendering the children. If the content cannot be interacted with and is essentially static maybe consider using a Pen to draw on the canvas instead. http://csharphelper.com/blog/2015/04/render-dashed-lines-in-a-wpf-program-using-c/ – bic Nov 23 '17 at 20:38
  • 1
    Also, the Pen can then be frozen (https://msdn.microsoft.com/en-us/library/system.windows.media.pen(v=vs.110).aspx) https://stackoverflow.com/questions/537950/why-does-use-of-pens-with-dash-patterns-cause-huge-performance-degredation-i – bic Nov 23 '17 at 20:39
  • Here is more information that may shed light on what you're trying to do https://stackoverflow.com/a/9996707/99804 – bic Nov 25 '17 at 21:03

0 Answers0