1

My main aim is to update drawingvisuals thicknesses when I zoom on canvas. It takes very long time to update each drawingvisual if there are too much drawing visuals (like 100.000 lines) in the canvas. So I decided to have a Pen property in my class then create the dravingvisuals with that pen. Pen thickness property is in binding with zoom ratio of canvas. This way zooming works whell, it is fast enough and thicknesses of draving visuals are being updated correctly. Problem on that point is creating 100000 lines takes too much time about 4 minutes.

public Pen pen { get; set; }

private void test()
{
    Point lastPoint=new Point(0,0);
    double lastAngle=0;

    for (int i = 1; i <100000; i++)
    {
       Line temp = new Line(lastPoint, lastAngle, 20);          

       _lineList.Add(temp);
       lastPoint = temp.Point2;
       lastAngle = lastAngle > 360 ? 0 : lastAngle + 30;            
    }

    foreach (Line l in _lineList)
    {               
      VisualAdd(l.Geometri(), l.CNID, pen);
    }
}

public void VisualAdd(Geometry g, int id, Pen p)
{
    DrawingVisual c = DrawingVisualCreate(g, id, p);
  _children.Add(DrawingVisualOlustur(g,id,p));
}



private DrawingVisual DrawingVisualCreate(Geometry g, int id,Pen p)
{                             
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    drawingContext.DrawGeometry(null, p, g);
    drawingContext.Close();
    drawingVisual.Id = id;
    return drawingVisual;
}

If I create the drawing visual like below to create 100000 lines takes only 2 seconds. But as I explain then to update the thickness while zooming is going to be big problem.

public void VisualAdd(Geometry g, int id)
{
    DrawingVisual c = DrawingVisualCreate(g, id);
  _children.Add(DrawingVisualOlustur(g,id,p));
}

private DrawingVisual DrawingVisualCreate(Geometry g, int id)
{                             
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    drawingContext.DrawGeometry(null, new Pen(Brushes.Black,1), g);
    drawingContext.Close();
    drawingVisual.Id = id;
    return drawingVisual;
}

I dont understand why it is so that slow when creating with a pen which is a created property.

Loktar
  • 34,764
  • 7
  • 90
  • 104
msnmz
  • 11
  • 2
  • Did you consider ScaleTransform for achieving zoom? – pushpraj Jun 23 '14 at 09:14
  • I use ScaleTransform and update it for the Canvas not all elements seperately. – msnmz Jun 23 '14 at 09:21
  • 1
    that is correct, so by right it will scale everything within it, I wonder why you would need to manipulate the thickness separately. – pushpraj Jun 23 '14 at 09:33
  • I want to have fixed thickness for shapes but when I zoom in thicknesses are also multiplied by scale factor which is depend on WPF canvas properties as a result of my searches. When I zoom in thickness becomes too wide and when zoom out it becomes almost not wisible thin. – msnmz Jun 23 '14 at 09:58
  • got it. I am wondering what you are developing where you need to draw 100,000 lines? is it possible for you to post a working sample? so I may have a look. I have done some similar optimizations in past, you may have a look on them in the mean while. see [Speed up adding objects to Canvas In WPF](http://stackoverflow.com/questions/23976163/speed-up-adding-objects-to-canvas-in-wpf/23976355#23976355) and [How to speed up rendering of vertical scrollbar markers](http://stackoverflow.com/questions/24318971/how-to-speed-up-rendering-of-vertical-scrollbar-markers/24319425#24319425) – pushpraj Jun 23 '14 at 11:17
  • Based on your information, my guess would be it has something to do that your `Pen.Thickness` is in a `Binding`. Try your first effort with just a locally instantiated pen to see if the creation is long. If it's not, it's likely something to do with the `Binding` - perhaps it's failing at creation time? – Chris Badman Jun 23 '14 at 11:28
  • I tried with binding and without binding are same. The difference happens only when I craeate Pen inside DrawingVisualAdd function that time it is creating fast but zooming becomes very very slow. If I pass Pen from outside of the function wheter it is propery or field or dependency property it takes long time for first creation, then it works normal. – msnmz Jun 23 '14 at 11:54

0 Answers0