Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF faster DrawingVisuals initialization.

WPF faster DrawingVisuals initialization.

Scheduled Pinned Locked Moved WPF
csharpdatabasewpfgraphicsperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Polykar
    wrote on last edited by
    #1

    Hello, We are planning to migrate to WPF. For a few use cases, drawingspeed is an issue. Drawing 10'000 mostly static lines on a zoomable/panable canvas is about as much as I need. Using DrawingVisuals, the performance is not acceptable, because the initialization takes around 10 seconds (which is ridiculous). After the initalization, the paning/zooming works fine. Please have a look at my sample code and tell me which part is terribly wrong...

            Pen pen = new Pen(Brushes.Red, 5);
            Random random = new Random();
    
            DrawingVisual visual = new DrawingVisual();
            using (DrawingContext dc = visual.RenderOpen())
            {
                for (int i = 0; i < 10000; i++)
                {
                    Point p1 = new Point(random.Next(0, 5000), random.Next(0, 5000));
                    Point p2 = new Point(random.Next(0, 100), random.Next(0, 100));
                    dc.DrawLine(pen, p1, new Point(p1.X - p2.X, p1.Y - p2.Y));
                }
                dc.Close();
            }            
            MyCanvas.AddVisual(visual);
    

    where MyCanvas is an overload of Canvas

    using System.Collections.Generic;
    using System.Windows.Controls;
    using System.Windows.Media;

    namespace WpfApplication1
    {
    public class DrawingCanvas : Canvas
    {
    private List _visuals = new List();

        protected override int VisualChildrenCount
        {
            get
            {
                return \_visuals.Count;
            }
        }
    
        protected override Visual GetVisualChild(int index)
        {
            return \_visuals\[index\];
        }
    
        public void AddVisual(Visual visual)
        {
            \_visuals.Add(visual);
            base.AddVisualChild(visual);
            base.AddLogicalChild(visual);
        }
    }
    

    }

    thanks a lot. regards. mike.

    L 1 Reply Last reply
    0
    • P Polykar

      Hello, We are planning to migrate to WPF. For a few use cases, drawingspeed is an issue. Drawing 10'000 mostly static lines on a zoomable/panable canvas is about as much as I need. Using DrawingVisuals, the performance is not acceptable, because the initialization takes around 10 seconds (which is ridiculous). After the initalization, the paning/zooming works fine. Please have a look at my sample code and tell me which part is terribly wrong...

              Pen pen = new Pen(Brushes.Red, 5);
              Random random = new Random();
      
              DrawingVisual visual = new DrawingVisual();
              using (DrawingContext dc = visual.RenderOpen())
              {
                  for (int i = 0; i < 10000; i++)
                  {
                      Point p1 = new Point(random.Next(0, 5000), random.Next(0, 5000));
                      Point p2 = new Point(random.Next(0, 100), random.Next(0, 100));
                      dc.DrawLine(pen, p1, new Point(p1.X - p2.X, p1.Y - p2.Y));
                  }
                  dc.Close();
              }            
              MyCanvas.AddVisual(visual);
      

      where MyCanvas is an overload of Canvas

      using System.Collections.Generic;
      using System.Windows.Controls;
      using System.Windows.Media;

      namespace WpfApplication1
      {
      public class DrawingCanvas : Canvas
      {
      private List _visuals = new List();

          protected override int VisualChildrenCount
          {
              get
              {
                  return \_visuals.Count;
              }
          }
      
          protected override Visual GetVisualChild(int index)
          {
              return \_visuals\[index\];
          }
      
          public void AddVisual(Visual visual)
          {
              \_visuals.Add(visual);
              base.AddVisualChild(visual);
              base.AddLogicalChild(visual);
          }
      }
      

      }

      thanks a lot. regards. mike.

      L Offline
      L Offline
      lneir
      wrote on last edited by
      #2

      This is an old discussion but might be helpful for you: drawingcontext very slow many geometries

      P 1 Reply Last reply
      0
      • L lneir

        This is an old discussion but might be helpful for you: drawingcontext very slow many geometries

        P Offline
        P Offline
        Polykar
        wrote on last edited by
        #3

        Thanks a lot. Freezing the Pen did the Trick.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups