zoukankan      html  css  js  c++  java
  • WPF绘图性能问题

    代码:

        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DrawPanel chart_ = new DrawPanel();
                LineCanvas _mask = new LineCanvas();
                grid1.Children.Add(chart_);
                grid1.Children.Add(_mask);
                chart_.plot();
            }
        }
        public class LineCanvas : Canvas
        {
            public LineCanvas()
            {
                Background = Brushes.Transparent;
                var line = new Line()
                {
                    StrokeThickness = 2,
                    Stroke = System.Windows.Media.Brushes.Black,
                };
    
                this.Children.Add(line);
                MouseMove += (send, e) =>
                  {
                      Point point = e.GetPosition(this);
                      line.X1 = point.X;
                      line.Y1 = 0;
                      line.X2 = point.X;
                      line.Y2 = ActualHeight;
                  };
            }
        }
    
        public class DrawPanel : Panel
        {
            private DrawingVisual _drawingVisual = new DrawingVisual();
            public DrawPanel()
            {
                this.AddVisualChild(_drawingVisual);
                IsHitTestVisible = false;
            }
            public Pen p = new Pen(Brushes.Gainsboro, 1.0);
            public void plot()
            {
                p.Freeze();
                var dc = _drawingVisual.RenderOpen();
                _drawingVisual.CacheMode = new BitmapCache();//这句话是关键,加了性能直接提升
                for (int x = 0; x < 10000; x++)
                {
                    double xx = 700.0 / 2 + ((x / 10000.0) * (700.0 / 2.0));
                    Point p1 = new Point(xx, 0);
                    Point p2 = new Point(xx, 353);
                    dc.DrawLine(p, p1, p2);
                }
                dc.Close();
            }
            protected override int VisualChildrenCount
            {
                get { return 1; }
            }
            protected override Visual GetVisualChild(int index)
            {
                if (index == 0)
                    return _drawingVisual;
                throw new IndexOutOfRangeException();
            }
        }

  • 相关阅读:
    项目总结
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    浅谈Vue与swiper轮播图框架结合小案例
  • 原文地址:https://www.cnblogs.com/kevinWu7/p/10163486.html
Copyright © 2011-2022 走看看