【发布时间】:2011-02-16 18:53:02
【问题描述】:
我正在研究 .NET 3.5 中的元胞自动机生成器。我决定使用 WPF 是因为我想在 WPF 中学习一些新东西。
我画了一维自动机,如规则 30 和二维自动机,如 Life。
我需要一些东西来快速绘制许多图像。
例如,网格尺寸为 64 x 64,单元格尺寸为 12px。所以我一步画出了 64*64 = 4096 张图片。每一步之间的间隔约为100毫秒。
我将我的应用程序重写为 WinForms,一切正常。但是在 WPF 中很慢,我不知道为什么。
我在 WPF 中的绘图示例:
我有一个派生自 Image 的类。在这个类中,我绘制了一个位图,并在 Source 属性中使用它。
public void DrawAll()
{
DrawingGroup dg = new DrawingGroup();
for (byte i = 0; i < this.DimensionX; ++i)
{
for (byte j = 0; j < this.DimensionY; ++j)
{
Piece p = this.Mesh[i][j];
Rect rect = new Rect(j * this.CellSize + (j + 1), i * this.CellSize + (i + 1),
this.CellSize, this.CellSize);
ImageDrawing id = new ImageDrawing();
id.Rect = rect;
if (p == null)
id.ImageSource = this._undefinedPiece.Image.Source;
else
id.ImageSource = p.Image.Source;
dg.Children.Add(id);
}
}
DrawingImage di = new DrawingImage(dg);
this.Source = di;
}
在 WPF 中绘图的第二个示例:
我从 Canvas 派生类并覆盖 OnRender 函数。基于这篇文章:http://sachabarber.net/?p=675
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
for (byte i = 0; i < this.DimensionX; ++i)
{
for (byte j = 0; j < this.DimensionY; ++j)
{
Rect rect = new Rect(j * this.CellSize + (j + 1), i * this.CellSize + (i + 1),
this.CellSize, this.CellSize);
BitmapImage bi;
int counter = i + j + DateTime.Now.Millisecond;
if (counter % 3 == 0)
bi = this.bundefined;
else if (counter % 3 == 1)
bi = this.bwhite;
else
bi = this.bred;
dc.DrawImage(bi, rect);
++counter;
}
}
}
感谢所有回复
【问题讨论】: