WPF画心电图

  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

时间: 2024-10-06 15:38:58

WPF画心电图的相关文章

WPF画箭头

前段时间,因工作需要利用WPF画箭头,在参考Using WPF to Visualize a Graph with Circular Dependencies后.自己写了一个WPF画箭头的库. 先上效果图, XAML代码如下: <Window x:Class="WPFArrows.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="

WPF画线问题,几千条以后就有明显的延迟了。

我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() {    _path.Data = _lines; } public Draw() {   LineGeometry line = new LineGeometry(p1, p2);   _lines.Children.Add(line); } }一开始的速度很好,但是线多了以后,就有明显的延迟了. 有什么更快速的方法不? 解决方案 ?

Wpf 画刷

画刷类 1.SolidColorBrush 使用单一的连续颜色绘制区域. 2.LinearGradientBrush 使用简便填充绘制区域,渐变的阴影填充从一种颜色变化到另一种颜色. 3.RadialGradientBrush 使用径向简便填充绘制区域,除了是在圆形模式中从中心点向外部辐射渐变之外,这种画刷和线性检变化刷类似. 4.ImageBrush 使用可以被拉伸.缩放或平铺的图像绘制区域. 5.DrawingBrush 使用一个Drawing对象绘制区域.该对象可以包含已经定义的形状和位图

wpf 画刷的分类

System.Windows.Media.Brush最上一层画刷 System.Windows.Media.GradientBrush  线性画刷 ,下层主要有两种画刷 System.Windows.Media.LinearGradientBrush  线性渐变 System.Windows.Media.RadialGradientBrush焦点定义渐变的开变 System.Windows.Media.BitmapCacheBrush  使用缓存的内容绘制区域 System.Windows.Me

WPF 画刷应用

纯色: SolidColorBrush brush = new SolidColorBrush(Colors.White); window1.Background = brush; 渐变色: LinearGradientBrush buttonBrush = new LinearGradientBrush(); buttonBrush.StartPoint = new Point(0, 0.5); buttonBrush.EndPoint = new Point(1, 0.5); buttonB

WPF 如何画一颗心

原文:WPF 如何画一颗心 如何用WPF画一个心. MainWindow.xaml <Window x:Class="Heart.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://sch

WPF中使用VisualBrush的实例

本文实现一个名为"你来我往"的小程序,该程序管理着"张三"和"李四"两位童鞋拥有的现金,一开始,两人均拥有100美元的现金,随着将现金从其中一人转移至另外一人,两人拥有的现金数在不断变化,程序可以跟踪这种变化,并正确显示每人拥有的现金数.每次最多可以转移三张纸币,纸币的金额可以是5美元.10美元或者20美元. 程序运行后的效果如图1所示,我们点击"张三"右边的"5美元""10美元"&qu

【开源】XPShadow, 用阴影让UWP更有层次感

UWP采用的是纯扁平化的设计,个人感觉极端了点,整个世界都是平的,导致App分不清层次,看不出重点.其实扁平化是趋势,android, ios都在搞,问题是android, ios都可以很轻松的实现阴影来突出重点和分层,android的material design更是火了一通,其中也大量用到阴影(card, float button等). 做UWP应用的时候就想WinRT用的xaml和WPF差不多,WPF画阴影很轻松,WinRT应该也很方便吧,结果查了半天资料,硬是找不到解决方案. 只能自己来

win10 uwp 渲染原理 DirectComposition 渲染

本文来告诉大家一个新的技术DirectComposition,在 win7 之后(实际上是 vista),微软正在考虑一个新的渲染机制 在 Windows Vista 就引入了一个服务,桌面窗口管理器Desktop Window Manager,虽然从借助 C++ 进行 Windows 开发博客可以看到 DWM 不是一个好的方法,但是比之前好. 在 win8 的时候,微软提出了 DirectComposition ,这是一个新的方法. 在软件的渲染一直都是两个阵营,一个是使用直接渲染模式.直接渲