如果线较多时,在画布中用Path或Line生成时会比较慢。用DrawingVisual可以快速生成,这个在之前我的博客中已经提到。但在类库形式下生成的无法看到,保存成Image后再加入图层后成功显示。
DrawingVisual drawingVisual = new DrawingVisual(); DrawingContext drawingContext = drawingVisual.RenderOpen(); Pen pen = new Pen(Brushes.DarkGray, 0.5); pen.Freeze(); meshLines.ForEach(line => drawingContext.DrawLine(pen, new System.Windows.Point(line.X1, line.Y1), new System.Windows.Point(line.X2, line.Y2))); drawingContext.Close(); RenderTargetBitmap bmp = new RenderTargetBitmap(Convert.ToInt32(_ucCanvas.MainCanvas.ActualWidth), Convert.ToInt32(_ucCanvas.MainCanvas.ActualHeight), 96, 96, PixelFormats.Pbgra32); bmp.Render(drawingVisual); _floorImage = new System.Windows.Controls.Image(); _floorImage.Source = bmp; _floorImage.Width = _ucCanvas.MainCanvas.ActualWidth; _floorImage.Height = _ucCanvas.MainCanvas.ActualHeight;