当前位置:   article > 正文

关于WPF画图性能问题_wpf stroke 太多 卡顿

wpf stroke 太多 卡顿

最近用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的热心帮助下,终于找到了方法,看他的代码:

  1. public class WriteableBitmapTrendLine : FrameworkElement
  2. {
  3. #region DependencyProperties
  4. public static readonly DependencyProperty LatestQuoteProperty =
  5. DependencyProperty.Register("LatestQuote", typeof(MinuteQuoteViewModel), typeof(WriteableBitmapTrendLine),
  6. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnLatestQuotePropertyChanged));
  7. private static void OnLatestQuotePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  8. {
  9. WriteableBitmapTrendLine trendLine = (WriteableBitmapTrendLine)d;
  10. MinuteQuoteViewModel latestQuote = (MinuteQuoteViewModel)e.NewValue;
  11. if (latestQuote != null)
  12. {
  13. trendLine.DrawTrendLine(latestQuote.Ordinal, (float)latestQuote.LastPx);
  14. }
  15. }
  16. public MinuteQuoteViewModel LatestQuote
  17. {
  18. get { return (MinuteQuoteViewModel)GetValue(LatestQuoteProperty); }
  19. set { SetValue(LatestQuoteProperty, value); }
  20. }
  21. #endregion
  22. private const int COLS = 723;
  23. private const int ROWS = 41;
  24. private WriteableBitmap bitmap;
  25. private float maxPrice = 0.0F;
  26. private static int dx = 3;
  27. private float[] prices = new float[COLS / dx];
  28. public WriteableBitmapTrendLine()
  29. {
  30. this.bitmap = new WriteableBitmap(COLS, ROWS, 96, 96, PixelFormats.Rgb24, null);
  31. this.bitmap.Lock();
  32. using (Bitmap backBufferBitmap = new Bitmap(COLS, ROWS,
  33. this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb,
  34. this.bitmap.BackBuffer))
  35. {
  36. using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap))
  37. {
  38. backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke);
  39. backBufferGraphics.Flush();
  40. }
  41. }
  42. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, COLS, ROWS));
  43. this.bitmap.Unlock();
  44. }
  45. private void DrawTrendLine(int ordinal, float latestPrice)
  46. {
  47. if (double.IsNaN(latestPrice))
  48. return;
  49. this.prices[ordinal] = latestPrice;
  50. bool redraw = false;
  51. if (ordinal == 0)
  52. {
  53. this.maxPrice = latestPrice;
  54. }
  55. else
  56. {
  57. if (latestPrice > this.maxPrice)
  58. {
  59. this.maxPrice = latestPrice;
  60. redraw = true;
  61. }
  62. }
  63. if (ordinal == 0)
  64. {
  65. int width = this.bitmap.PixelWidth;
  66. int height = this.bitmap.PixelHeight;
  67. this.bitmap.Lock();
  68. using (Bitmap backBufferBitmap = new Bitmap(width, height,
  69. this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb,
  70. this.bitmap.BackBuffer))
  71. {
  72. using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap))
  73. {
  74. backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke);
  75. backBufferGraphics.Flush();
  76. }
  77. }
  78. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
  79. this.bitmap.Unlock();
  80. }
  81. else
  82. {
  83. System.Drawing.Point[] points = new System.Drawing.Point[ordinal + 1];
  84. float dy = (float)(ROWS / (this.maxPrice * 1.3));
  85. for (int i = 0; i <= ordinal; i++)
  86. {
  87. points[i].X = i * dx;
  88. points[i].Y = (int)(this.prices[i] * dy);
  89. }
  90. int width = ordinal * dx + 1;
  91. int height = this.bitmap.PixelHeight;
  92. this.bitmap.Lock();
  93. using (Bitmap backBufferBitmap = new Bitmap(width, height,
  94. this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb,
  95. this.bitmap.BackBuffer))
  96. {
  97. using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap))
  98. {
  99. backBufferGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
  100. backBufferGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
  101. if (redraw)
  102. backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke);
  103. backBufferGraphics.DrawLines(System.Drawing.Pens.Green, points);
  104. backBufferGraphics.Flush();
  105. }
  106. }
  107. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
  108. this.bitmap.Unlock();
  109. }
  110. }
  111. private void DrawTrendLineF(int ordinal, float latestPrice)
  112. {
  113. if (double.IsNaN(latestPrice))
  114. return;
  115. this.prices[ordinal] = latestPrice;
  116. if (ordinal == 0)
  117. {
  118. this.maxPrice = latestPrice;
  119. }
  120. else
  121. {
  122. if (latestPrice > this.maxPrice)
  123. {
  124. this.maxPrice = latestPrice;
  125. }
  126. }
  127. if (ordinal == 0)
  128. {
  129. int width = this.bitmap.PixelWidth;
  130. int height = this.bitmap.PixelHeight;
  131. this.bitmap.Lock();
  132. using (Bitmap backBufferBitmap = new Bitmap(width, height,
  133. this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb,
  134. this.bitmap.BackBuffer))
  135. {
  136. using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap))
  137. {
  138. backBufferGraphics.Clear(System.Drawing.Color.WhiteSmoke);
  139. backBufferGraphics.Flush();
  140. }
  141. }
  142. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
  143. this.bitmap.Unlock();
  144. }
  145. else
  146. {
  147. int count = this.prices.Length;
  148. PointF[] points = new PointF[ordinal + 1];
  149. float dy = (float)(ROWS / this.maxPrice);
  150. for (int i = 0; i <= ordinal; i++)
  151. {
  152. points[i].X = i;
  153. points[i].Y = (float)Math.Floor(this.prices[i] * dy);
  154. if (float.IsNaN(points[i].Y))
  155. points[i].Y = 0.0F;
  156. }
  157. int width = ordinal + 1;
  158. int height = this.bitmap.PixelHeight;
  159. this.bitmap.Lock();
  160. using (Bitmap backBufferBitmap = new Bitmap(width, height,
  161. this.bitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb,
  162. this.bitmap.BackBuffer))
  163. {
  164. using (Graphics backBufferGraphics = Graphics.FromImage(backBufferBitmap))
  165. {
  166. backBufferGraphics.DrawLines(System.Drawing.Pens.Green, points);
  167. backBufferGraphics.Flush();
  168. }
  169. }
  170. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
  171. this.bitmap.Unlock();
  172. }
  173. }
  174. protected override void OnRender(DrawingContext drawingContext)
  175. {
  176. drawingContext.PushTransform(new ScaleTransform(1, -1, 0, RenderSize.Height / 2));
  177. drawingContext.DrawImage(bitmap, new Rect(0, 0, RenderSize.Width, RenderSize.Height));
  178. }
  179. }
  1. public class MinuteQuoteViewModel : INotifyPropertyChanged
  2. {
  3. private int ordinal;
  4. public int Ordinal
  5. {
  6. get { return this.ordinal; }
  7. set { if (this.ordinal != value) { this.ordinal = value; this.OnPropertyChanged("Ordinal"); } }
  8. }
  9. private DateTime quoteTime;
  10. public DateTime QuoteTime
  11. {
  12. get { return this.quoteTime; }
  13. set { if (this.quoteTime != value) { this.quoteTime = value; this.OnPropertyChanged("QuoteTime"); } }
  14. }
  15. private double lastPx = double.NaN;
  16. public double LastPx
  17. {
  18. get { return this.lastPx; }
  19. set { if (this.lastPx != value) { this.lastPx = value; this.OnPropertyChanged("LastPx"); } }
  20. }
  21. private double avgPx = double.NaN;
  22. public double AvgPx
  23. {
  24. get { return this.avgPx; }
  25. set { if (this.avgPx != value) { this.avgPx = value; this.OnPropertyChanged("AvgPx"); } }
  26. }
  27. private int volume;
  28. public int Volume
  29. {
  30. get { return this.volume; }
  31. set { if (this.volume != value) { this.volume = value; this.OnPropertyChanged("Volume"); } }
  32. }
  33. private double amount = double.NaN;
  34. public double Amount
  35. {
  36. get { return this.amount; }
  37. set { if (this.amount != value) { this.amount = value; this.OnPropertyChanged("Amount"); } }
  38. }
  39. #region INotifyPropertyChanged 成员
  40. public event PropertyChangedEventHandler PropertyChanged;
  41. protected virtual void OnPropertyChanged(string propertyName)
  42. {
  43. if (this.PropertyChanged != null)
  44. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  45. }
  46. #endregion
  47. }


他这个应该是直接从项目上拔下来的,看着比较费劲,我在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


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/191454?site
推荐阅读
相关标签
  

闽ICP备14008679号