C# 如何在 0.5 秒内用 WPF 绘制一万个点的线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1150060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw line of ten thousands of points with WPF within 0.5 second?
提问by
I am writing WPFcode to show a real-time plot which is a connected line containing about 10,000 points. It takes about 5 seconds to show a picture in my computer. Does anyone have an idea to make it quicker and within 0.5 second?
我正在编写WPF代码来显示一个实时图,它是一条包含大约 10,000 个点的连接线。在我的电脑中显示一张图片大约需要 5 秒钟。有没有人想在 0.5 秒内让它更快?
class eee : FrameworkElement
{
public eee()
{
_children = new VisualCollection(this);
Random rand = new Random();
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dx = dv.RenderOpen())
{
Pen drawingPen = new Pen(Brushes.Black, 1);
double x=rand.Next(300);
double y = rand.Next(300);
for (double i = 0; i < 1000; i = i + 0.1)
{
y = 100 + rand.Next(100);
dx.DrawLine(drawingPen, new Point(i, x), new Point(i + 1, y));
x = y;
}
}
_children.Add(dv);
}
回答by RandomNickName42
Charles Petzolddoes exactly that. It is even faster on my host (< 0.3 secs), and the point's are even DataBound!! ;)
Charles Petzold正是这样做的。在我的主机上它甚至更快(< 0.3 秒),而且这一点甚至是 DataBound !!;)
Tamir Khasondoes this also, with lines and goes into more depth about Bitmap style performance WPF here.
Tamir Khason也这样做了,在此处使用线条并更深入地了解位图样式性能 WPF 。
Rico Marianihas some guidance for 3D high performance graphics, essentially leveraging value types can improve your throughput if well thought out.
Rico Mariani为 3D 高性能图形提供了一些指导,如果经过深思熟虑,基本上利用值类型可以提高您的吞吐量。
Jianzhong Zhanggives my new favourate tutorials on this subject, 3D scatter plot several tens of thousands of data points animated and interactive.
张建中在这个主题上给出了我最喜欢的新教程,3D 散点图数万个动画和交互式数据点。
回答by Arsen Mkrtchyan
Should the lines be selectable? You can draw lines in the image, then give it as a source to Image control. It will draw faster but you will lose ability to interact with lines.
行应该是可选择的吗?您可以在图像中绘制线条,然后将其作为图像控件的源。它会画得更快,但你将失去与线条交互的能力。
回答by Jonke
I guess the code sample is 1) a test to try a something that isn't really the sample or 2) a homework.
我猜代码示例是 1) 尝试尝试一些不是真正示例的东西的测试或 2) 作业。
Try to override the OnRender and do something like:
尝试覆盖 OnRender 并执行以下操作:
Pen drawingPen = new Pen(Brushes.Black, 1);
protected override void OnRender(DrawingContext dc)
{
dc.DrawRectangle(Background, null, new Rect(RenderSize));
double x=rand.Next(300);
double y = rand.Next(300);
for (double i = 0; i < 1000; i = i + 0.1)
{
y = 100 + rand.Next(100);
dc.DrawLine(drawingPen, new Point(i, x), new Point(i + 1, y));
x = y;
}
}
or for something with real data, consider if you really need to show every point depending on the resolution of the visual context. ( If your scale is 0-10 and you are producing points 0.0001,0.00015 are they really gone differ on your scale)
或者对于具有真实数据的内容,请考虑是否真的需要根据视觉上下文的分辨率显示每个点。(如果您的量表是 0-10 并且您产生的点数是 0.0001,0.00015,它们是否真的在您的量表上有所不同)