我正在研究.NET 3.5中的细胞自动机生成器.我决定使用WPF因为我想学习WPF中的新东西.
我像规则30和二维像生命一样绘制一维自动机.
我需要一些东西来快速绘制许多图像.
例如,网格的尺寸是64×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;
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢所有回复
WPF 真正的力量在于渲染向量。有没有一种方法可以不使用图像,而是创建预定大小的矩形对象并分配颜色,然后分配位置?与矢量相比,位图需要大量资源。尽管 WPF 将渲染推到显卡上,但仍然需要大量处理。
| 归档时间: |
|
| 查看次数: |
2245 次 |
| 最近记录: |