Ole*_*evs 9 c# wpf performance drawing
我在WPF绘图性能方面遇到问题.有许多小的EllipseGeometry对象(例如1024个椭圆),它们被添加到具有不同前景画笔的三个单独的GeometryGroup中.之后,我在简单的图像控件上渲染它.码:
DrawingGroup tmpDrawing = new DrawingGroup();
GeometryGroup onGroup = new GeometryGroup();
GeometryGroup offGroup = new GeometryGroup();
GeometryGroup disabledGroup = new GeometryGroup();
for (int x = 0; x < DisplayWidth; ++x)
{
for (int y = 0; y < DisplayHeight; ++y)
{
if (States[x, y] == true) onGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
else if (States[x, y] == false) offGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
else disabledGroup.Children.Add(new EllipseGeometry(new Rect((double)x * EDGE, (double)y * EDGE, EDGE, EDGE)));
}
}
tmpDrawing.Children.Add(new GeometryDrawing(OnBrush, null, onGroup));
tmpDrawing.Children.Add(new GeometryDrawing(OffBrush, null, offGroup));
tmpDrawing.Children.Add(new GeometryDrawing(DisabledBrush, null, disabledGroup));
DisplayImage.Source = new DrawingImage(tmpDrawing);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但需要太多时间 - 在Core 2 Quad上> 0.5s,在Pentium 4上> 2s.我需要<0.1s无处不在.你可以看到的所有椭圆都是平等的.控件的背景,我的DisplayImage,是固体(例如黑色),所以我们可以使用这个事实.我尝试使用1024个Ellipse元素而不是使用EllipseGeometries的Image,并且它工作得更快(~0.5s),但还不够.如何加快它?
此致,Oleg Eremeev
PS抱歉我的英文.
我保留了旧的渲染方法,但是每次创建一个新的EllipseGeometry对象都是一个坏主意,因此我以这种方式对其进行了优化:
for (int x = 0; x < newWidth; ++x)
{
for (int y = 0; y < newHeight; ++y)
{
States[x, y] = null;
OnEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
OffEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
DisabledEllipses[x, y] = new EllipseGeometry(new Rect((double)x * EDGE + 0.5f, (double)y * EDGE + 0.5f, EDGE - 1f, EDGE - 1f));
}
}
// . . .
DrawingGroup tmpDrawing = new DrawingGroup();
GeometryGroup onGroup = new GeometryGroup();
GeometryGroup offGroup = new GeometryGroup();
GeometryGroup disabledGroup = new GeometryGroup();
for (int x = 0; x < DisplayWidth; ++x)
{
for (int y = 0; y < DisplayHeight; ++y)
{
if (States[x, y] == true) onGroup.Children.Add(OnEllipses[x, y]);
else if (States[x, y] == false) offGroup.Children.Add(OffEllipses[x, y]);
else disabledGroup.Children.Add(DisabledEllipses[x, y]);
}
}
tmpDrawing.Children.Add(new GeometryDrawing(OnBrush, null, onGroup));
tmpDrawing.Children.Add(new GeometryDrawing(OffBrush, null, offGroup));
tmpDrawing.Children.Add(new GeometryDrawing(DisabledBrush, null, disabledGroup));
DisplayImage.Source = new DrawingImage(tmpDrawing);
Run Code Online (Sandbox Code Playgroud)
对于x = 128和y = 8,即使在奔腾III系统上,它的工作速度也非常快。