关于WPF画图性能问题
2014-11-24 00:00
239 查看
最近用wpf画心电图,尝试了wpf所有的方法,性能依然不能满足要求,后来发现舍本逐末了,现在记录下来,以免以后再走弯路。
首先要明白wpf管理的机制,如果你往canvas画一条线,一般就是 new Line() 然后添加到canvas里面,这样做的话就算你用轻量级的Polyline,或者使用DrawingVisual的方法。对于高频数据来说(比如心电波形)都会很卡,这里面使用inkcanvas添加stroke的方法效果最好。但是如果你过多执行strokes的clear()和Add,时间一长,内存就会爆掉,InkCanvas 只能胜任轻量级的工作,WPF 会管理 InkCanvas 中的所有元素,例如位置和尺寸, 所以它会消耗大量的内存和 CPU处理时间。InkCanvas 会渲染所有的stroke 在其装饰器层(Adorner Layer)。
可能我对wpf的认识还不足,硬生的把wpf和GDI+隔离开来看了,后来在Just Wang的热心帮助下,终于找到了方法,看他的代码:
他这个应该是直接从项目上拔下来的,看着比较费劲,我在vs2013下写了一个简单的示例,取消了固定区域,转为自适应界面。
代码下载:http://download.csdn.net/detail/waleswood/7079287
相关帖子地址:http://social.msdn.microsoft.com/Forums/zh-CN/3baebf07-5a0e-4e3a-a588-b79d869d6d47/inkcanvasstrokesclear?forum=wpfzhchs#7ce0efd9-7254-4d98-9d86-427d820cd827
http://social.msdn.microsoft.com/Forums/zh-CN/febcee07-dc8b-44b4-8c0a-246daffdbe2b/wpf-?forum=wpfzhchs#bfadef47-ab7b-4f8b-9340-e3c7a2782b76
http://social.msdn.microsoft.com/Forums/zh-CN/b156e12b-bc52-44a9-b2f9-26fff723cff5/wpf-inkcanvas?forum=wpfzhchs#de6c4b50-7036-4823-bbec-4e9ba309a600
首先要明白wpf管理的机制,如果你往canvas画一条线,一般就是 new Line() 然后添加到canvas里面,这样做的话就算你用轻量级的Polyline,或者使用DrawingVisual的方法。对于高频数据来说(比如心电波形)都会很卡,这里面使用inkcanvas添加stroke的方法效果最好。但是如果你过多执行strokes的clear()和Add,时间一长,内存就会爆掉,InkCanvas 只能胜任轻量级的工作,WPF 会管理 InkCanvas 中的所有元素,例如位置和尺寸, 所以它会消耗大量的内存和 CPU处理时间。InkCanvas 会渲染所有的stroke 在其装饰器层(Adorner Layer)。
可能我对wpf的认识还不足,硬生的把wpf和GDI+隔离开来看了,后来在Just Wang的热心帮助下,终于找到了方法,看他的代码:
public class WriteableBitmapTrendLine : FrameworkElement { #region DependencyProperties public static readonly DependencyProperty LatestQuoteProperty = DependencyProperty.Register("LatestQuote", typeof(MinuteQuoteViewModel), typeof(WriteableBitmapTrendLine), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnLatestQuotePropertyChanged)); private static void OnLatestQuotePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { WriteableBitmapTrendLine trendLine = (WriteableBitmapTrendLine)d; MinuteQuoteViewModel latestQuote = (MinuteQuoteViewModel)e.NewValue; if (latestQuote != null) { trendLine.DrawTrendLine(latestQuote.Ordinal, (float)latestQuote.LastPx); } } public MinuteQuoteViewModel LatestQuote { get { return (MinuteQuoteViewModel)GetValue(LatestQuoteProperty); } set { SetValue(LatestQuoteProperty, value); } } #endregion private const int COLS = 723; private const int ROWS = 41; private WriteableBitmap bitmap; private float maxPrice = 0.0F; private static int dx = 3; private float[] prices = new float[COLS / dx]; public WriteableBitmapTrendLine() { this.bitmap = new WriteableBitmap(COLS, ROWS, 96, 96, PixelFormats.Rgb24, null); this.bitmap.Lock(); using (Bitmap backBufferBitmap = new Bitmap(COLS, ROWS, this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, this.bitmap.BackBuffer)) { using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap)) { backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke); backBufferGraphics.Flush(); } } this.bitmap.AddDirtyRect(new Int32Rect(0, 0, COLS, ROWS)); this.bitmap.Unlock(); } private void DrawTrendLine(int ordinal, float latestPrice) { if (double.IsNaN(latestPrice)) return; this.prices[ordinal] = latestPrice; bool redraw = false; if (ordinal == 0) { this.maxPrice = latestPrice; } else { if (latestPrice > this.maxPrice) { this.maxPrice = latestPrice; redraw = true; } } if (ordinal == 0) { int width = this.bitmap.PixelWidth; int height = this.bitmap.PixelHeight; this.bitmap.Lock(); using (Bitmap backBufferBitmap = new Bitmap(width, height, this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, this.bitmap.BackBuffer)) { using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap)) { backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke); backBufferGraphics.Flush(); } } this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); this.bitmap.Unlock(); } else { System.Drawing.Point[] points = new System.Drawing.Point[ordinal + 1]; float dy = (float)(ROWS / (this.maxPrice * 1.3)); for (int i = 0; i <= ordinal; i++) { points[i].X = i * dx; points[i].Y = (int)(this.prices[i] * dy); } int width = ordinal * dx + 1; int height = this.bitmap.PixelHeight; this.bitmap.Lock(); using (Bitmap backBufferBitmap = new Bitmap(width, height, this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, this.bitmap.BackBuffer)) { using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap)) { backBufferGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; backBufferGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; if (redraw) backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke); backBufferGraphics.DrawLines(System.Drawing.Pens.Green, points); backBufferGraphics.Flush(); } } this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); this.bitmap.Unlock(); } } private void DrawTrendLineF(int ordinal, float latestPrice) { if (double.IsNaN(latestPrice)) return; this.prices[ordinal] = latestPrice; if (ordinal == 0) { this.maxPrice = latestPrice; } else { if (latestPrice > this.maxPrice) { this.maxPrice = latestPrice; } } if (ordinal == 0) { int width = this.bitmap.PixelWidth; int height = this.bitmap.PixelHeight; this.bitmap.Lock(); using (Bitmap backBufferBitmap = new Bitmap(width, height, this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, this.bitmap.BackBuffer)) { using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap)) { backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke); backBufferGraphics.Flush(); } } this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); this.bitmap.Unlock(); } else { int count = this.prices.Length; PointF[] points = new PointF[ordinal + 1]; float dy = (float)(ROWS / this.maxPrice); for (int i = 0; i <= ordinal; i++) { points[i].X = i; points[i].Y = (float)Math.Floor(this.prices[i] * dy); if (float.IsNaN(points[i].Y)) points[i].Y = 0.0F; } int width = ordinal + 1; int height = this.bitmap.PixelHeight; this.bitmap.Lock(); using (Bitmap backBufferBitmap = new Bitmap(width, height, this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, this.bitmap.BackBuffer)) { using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap)) { backBufferGraphics.DrawLines(System.Drawing.Pens.Green, points); backBufferGraphics.Flush(); } } this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height)); this.bitmap.Unlock(); } } protected override void OnRender(DrawingContext drawingContext) { drawingContext.PushTransform(new ScaleTransform(1, -1, 0, RenderSize.Height / 2)); drawingContext.DrawImage(bitmap, new Rect(0, 0, RenderSize.Width, RenderSize.Height)); } }
public class MinuteQuoteViewModel : INotifyPropertyChanged { private int ordinal; public int Ordinal { get { return this.ordinal; } set { if (this.ordinal != value) { this.ordinal = value; this.OnPropertyChanged("Ordinal"); } } } private DateTime quoteTime; public DateTime QuoteTime { get { return this.quoteTime; } set { if (this.quoteTime != value) { this.quoteTime = value; this.OnPropertyChanged("QuoteTime"); } } } private double lastPx = double.NaN; public double LastPx { get { return this.lastPx; } set { if (this.lastPx != value) { this.lastPx = value; this.OnPropertyChanged("LastPx"); } } } private double avgPx = double.NaN; public double AvgPx { get { return this.avgPx; } set { if (this.avgPx != value) { this.avgPx = value; this.OnPropertyChanged("AvgPx"); } } } private int volume; public int Volume { get { return this.volume; } set { if (this.volume != value) { this.volume = value; this.OnPropertyChanged("Volume"); } } } private double amount = double.NaN; public double Amount { get { return this.amount; } set { if (this.amount != value) { this.amount = value; this.OnPropertyChanged("Amount"); } } } #region INotifyPropertyChanged 成员 public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion }
他这个应该是直接从项目上拔下来的,看着比较费劲,我在vs2013下写了一个简单的示例,取消了固定区域,转为自适应界面。
代码下载:http://download.csdn.net/detail/waleswood/7079287
相关帖子地址:http://social.msdn.microsoft.com/Forums/zh-CN/3baebf07-5a0e-4e3a-a588-b79d869d6d47/inkcanvasstrokesclear?forum=wpfzhchs#7ce0efd9-7254-4d98-9d86-427d820cd827
http://social.msdn.microsoft.com/Forums/zh-CN/febcee07-dc8b-44b4-8c0a-246daffdbe2b/wpf-?forum=wpfzhchs#bfadef47-ab7b-4f8b-9340-e3c7a2782b76
http://social.msdn.microsoft.com/Forums/zh-CN/b156e12b-bc52-44a9-b2f9-26fff723cff5/wpf-inkcanvas?forum=wpfzhchs#de6c4b50-7036-4823-bbec-4e9ba309a600
相关文章推荐
- 关于WPF画图性能问题
- 关于WPF画图性能问题
- WPF画图性能问题
- 关于WPF不规则窗体性能和大小更改问题探讨(转)
- [原]关于程序性能问题的一点想法
- 关于装箱的性能问题
- 关于Unix下面Java画图提示不能与XWindows连接的问题
- 综合应用WPF/WCF/WF/LINQ之二十二:关于单步调试存储过程的问题
- 关于大量数据写入数据库的性能问题
- 关于WPF的ComboBox中Items太多而导致加载过慢的问题(转载)
- 关于MS Sql Server性能问题
- 老生常谈:关于分页查询和性能问题
- PNG图片导致的WPF性能问题
- 我用的是stringgrid连接数据库!关于提高性能的问题
- 关于JavaScript解析XML的性能的问题(已解决)
- 关于require ,include 性能的问题
- 综合应用WPF/WCF/WF/LINQ之十七:关于简化发布新版本的步骤的问题
- WPF 遇到的性能问题
- 关于if嵌套性能的问题
- WPF 中Treeview 的性能问题