码迷,mamicode.com
首页 > Windows程序 > 详细

WPF画心电图

时间:2016-05-07 22:18:51      阅读:1097      评论:0      收藏:0      [点我收藏+]

标签:

  1 namespace WpfPaint
  2 {
  3     public class MinuteQuoteViewModel : INotifyPropertyChanged
  4     {
  5       
  6 
  7         private double lastPx = double.NaN;
  8         public double LastPx
  9         {
 10             get { return this.lastPx; }
 11             set { if (this.lastPx != value) { this.lastPx = value; this.OnPropertyChanged("LastPx"); } }
 12         }
 13 
 14         #region INotifyPropertyChanged 成员
 15 
 16         public event PropertyChangedEventHandler PropertyChanged;
 17         protected virtual void OnPropertyChanged(string propertyName)
 18         {
 19             if (this.PropertyChanged != null)
 20                 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 21         }
 22 
 23         #endregion
 24     }
 25 }
 26 
 27 
 28 
 29 
 30     public class WriteableBitmapTrendLine : FrameworkElement
 31     {
 32         #region DependencyProperties
 33 
 34         public static readonly DependencyProperty LatestQuoteProperty =
 35             DependencyProperty.Register("LatestQuote", typeof(MinuteQuoteViewModel), typeof(WriteableBitmapTrendLine),
 36             new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnLatestQuotePropertyChanged));
 37 
 38         private static void OnLatestQuotePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 39         {
 40             WriteableBitmapTrendLine trendLine = (WriteableBitmapTrendLine)d;
 41             MinuteQuoteViewModel latestQuote = (MinuteQuoteViewModel)e.NewValue;
 42             if (latestQuote != null)
 43             {
 44                 trendLine.DrawTrendLine((float)latestQuote.LastPx);
 45             }
 46         }
 47 
 48         public MinuteQuoteViewModel LatestQuote
 49         {
 50             get { return (MinuteQuoteViewModel)GetValue(LatestQuoteProperty); }
 51             set { SetValue(LatestQuoteProperty, value); }
 52         }
 53 
 54         #endregion
 55 
 56         private int width = 0;
 57         private int height = 0;
 58 
 59         private WriteableBitmap bitmap;
 60 
 61         /// <summary>
 62         /// 两点之间的距离
 63         /// </summary>
 64         private int dx = 5;
 65 
 66         /// <summary>
 67         /// 当前区域所容纳的值
 68         /// </summary>
 69         private float[] prices;
 70 
 71         /// <summary>
 72         /// 在prices中的索引
 73         /// </summary>
 74         private int ordinal = 0;
 75 
 76         private GDI.Pen pen = new GDI.Pen(GDI.Color.Black);
 77 
 78         private void DrawTrendLine(float latestPrice)
 79         {
 80             if (double.IsNaN(latestPrice))
 81                 return;
 82 
 83             ordinal++;
 84 
 85             if (ordinal > this.prices.Length - 1)
 86             {
 87                 ordinal = 0;
 88             }
 89             this.prices[ordinal] = latestPrice;
 90 
 91             this.bitmap.Lock();
 92 
 93             using (GDI.Bitmap backBufferBitmap = new GDI.Bitmap(width, height,
 94                 this.bitmap.BackBufferStride, GDI.Imaging.PixelFormat.Format24bppRgb,
 95                 this.bitmap.BackBuffer))
 96             {
 97                 using (GDI.Graphics backBufferGraphics = GDI.Graphics.FromImage(backBufferBitmap))
 98                 {
 99                     backBufferGraphics.SmoothingMode = GDI.Drawing2D.SmoothingMode.HighSpeed;
100                     backBufferGraphics.CompositingQuality = GDI.Drawing2D.CompositingQuality.HighSpeed;
101 
102 
103                     if (ordinal == 0)
104                     {
105                         backBufferGraphics.Clear(GDI.Color.White);
106                     }
107 
108                     for (int i = 0; i <= ordinal; i++)
109                     {
110                         if (ordinal > 0)
111                         {
112                             backBufferGraphics.DrawLine(pen,
113                                 new GDI.PointF((ordinal - 1) * dx, this.prices[ordinal - 1]),
114                                  new GDI.PointF(ordinal * dx, this.prices[ordinal]));
115                         }
116                     }
117                     backBufferGraphics.Flush();
118                 }
119             }
120             this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
121             this.bitmap.Unlock();
122         }
123 
124         protected override void OnRender(DrawingContext dc)
125         {
126             if (bitmap == null)
127             {
128                 this.width = (int)RenderSize.Width;
129                 this.height = (int)RenderSize.Height;
130                 this.bitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgr24, null);
131 
132                 this.bitmap.Lock();
133                 using (GDI.Bitmap backBufferBitmap = new GDI.Bitmap(width, height,
134                this.bitmap.BackBufferStride, GDI.Imaging.PixelFormat.Format24bppRgb,
135                this.bitmap.BackBuffer))
136                 {
137                     using (GDI.Graphics backBufferGraphics = GDI.Graphics.FromImage(backBufferBitmap))
138                     {
139                         backBufferGraphics.SmoothingMode = GDI.Drawing2D.SmoothingMode.HighSpeed;
140                         backBufferGraphics.CompositingQuality = GDI.Drawing2D.CompositingQuality.HighSpeed;
141 
142                         backBufferGraphics.Clear(GDI.Color.White);
143 
144                         backBufferGraphics.Flush();
145                     }
146                 }
147                 this.bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
148                 this.bitmap.Unlock();
149 
150                 this.prices = new float[(int)(this.width / this.dx)];
151             }
152             dc.DrawImage(bitmap, new Rect(0, 0, RenderSize.Width, RenderSize.Height));
153             base.OnRender(dc);
154         }
155     }

https://social.msdn.microsoft.com/Forums/zh-CN/3baebf07-5a0e-4e3a-a588-b79d869d6d47/inkcanvasstrokesclear?forum=wpfzhchs#7ce0efd9-7254-4d98-9d86-427d820cd827

 

https://social.msdn.microsoft.com/Forums/zh-CN/febcee07-dc8b-44b4-8c0a-246daffdbe2b/wpf-?forum=wpfzhchs#bfadef47-ab7b-4f8b-9340-e3c7a2782b76

 

https://social.msdn.microsoft.com/Forums/zh-CN/b156e12b-bc52-44a9-b2f9-26fff723cff5/wpf-inkcanvas?forum=wpfzhchs#de6c4b50-7036-4823-bbec-4e9ba309a600

WPF画心电图

标签:

原文地址:http://www.cnblogs.com/hwy425/p/5469227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!