1

I am building a map editor to easily create 2d levels. To visualize how my level looks like I am using a canvas. On this canvas I can select which block is filled with a certain type ( like solid ). I did add click and drag but because I am live drawing on the canvas it goes very slow and if you draw at a decent speed it skips several block which needs to be drawn.

I already have tried several options ( doing it in another thread. Putting it first in a queue ) but none had a good result.

I use the following code to draw my rectangles:

 if (currentMap != null)
        {
            mapCanvas.Children.Clear();
            mapCanvas.Width = (currentMap.Breedte * blockScale);
            mapCanvas.Height = (currentMap.Hoogte * blockScale);
            for (int i = 0; i < currentMap.Hoogte; i++)
            {
                for (int j = 0; j < currentMap.Breedte; j++)
                {
                    Rectangle blok = new Rectangle();

                    blok.Stroke = new SolidColorBrush(Colors.Black);
                    blok.StrokeThickness = 0.3;
                    blok.Width = blockScale;
                    blok.Height = blockScale;
                    switch (currentMap.GetElement(j, i))
                    {
                        case 0:
                            blok.Fill = new SolidColorBrush(Colors.LightGray);
                            break;
                        case 1:
                            blok.Fill = new SolidColorBrush(Colors.Red);
                            break;
                        case 2:
                            blok.Fill = new SolidColorBrush(Colors.Green);
                            break;
                        case 3:
                            blok.Fill = new SolidColorBrush(Colors.Orange);
                            break;
                        case 4:
                            blok.Fill = new SolidColorBrush(Colors.Yellow);
                            break;
                        default:
                            blok.Fill = new SolidColorBrush(Colors.Black);
                            break;

                    }
                    blok.SetValue(Canvas.LeftProperty, (double)(blockScale * j));
                    blok.SetValue(Canvas.TopProperty, (double)(blockScale * i));

                    mapCanvas.Children.Add(blok);
                }
            }
        }

I did came across a solution to draw faster on a canvas on this link: Speed up adding objects to Canvas In WPF

But the problem I cannot seem to get it working in my current project or a test project.

I hope you guys can help me to speed the drawing up or at least that it can recognize every block it needs to draw.

For further question please ask I will try to give as much detail as needed

Community
  • 1
  • 1
Joey Driessen
  • 318
  • 2
  • 14
  • Because of the loops this will be very slow, I'm not sure how this could be improved, but maybe you should ask it on code review instead of here. http://codereview.stackexchange.com/help/on-topic – Kyra Nov 06 '15 at 12:24
  • 1
    WPF is not made for this. This is not "drawing" at all, it is adding visuals to the tree which is slow. Draw to a bitmap and present the bitmap. – usr Nov 06 '15 at 12:28
  • You are using `.Clear` on your `Canvas` and then redrawing everything. Can't you just **move** the elements instead? – Mike Eason Nov 06 '15 at 12:29
  • I struggle with WPF [too](http://stackoverflow.com/q/25450979/1997232) and [now](http://stackoverflow.com/a/26138947/1997232) I am drawing into bitmap using old good GDI+. – Sinatr Nov 06 '15 at 12:47
  • Please, try other approaches. Instead of Fill, you can prepare different user controls and add them dynamically whenever u need. – Ugur Nov 06 '15 at 14:04

0 Answers0