您的位置:首页 > 其它

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: