我正在开发一个 Winforms 应用程序,可以使用一些建议。
随着时间的推移,我有数百个 50x50 的精灵在 2000x2000 的运动场上四处移动。最初,我用程序生成的图片框创建了它,这些图片框被添加到表单中并四处移动。它完成了工作,但它闪烁且缓慢。
经过相当多的谷歌搜索,它看起来像是创建一个帧缓冲区并直接绘制到该缓冲区,然后将缓冲区应用于表单上的静态图像框似乎是要走的路。
所以我把这一切都装好了,结果它比使用图片框要慢得多。这似乎是由于缓冲区2000x2000的大小(每次创建缓冲区大约需要100ms。)
绘制屏幕的代码:
private void animateAmoebas()
{
for (int animationStep = 0; animationStep < 100; animationStep = animationStep + animationStepSize)
{
Image buffer = new Bitmap(2000, 2000);
buffer = imageBKG; //Redraw the grid pattern.
foreach (Amoeba _Amoeba in amoebaPool)//Ameboa is a class object that has AI in it to detirmine the actions of the Amoeba.
{
//PBL (PictureBoxLoader) is an object that contains the sprite image, plus the cordinates for that sprite in that frame.
pbl = _Amoeba.animateSprite(animationStep,pbl);
drawSprite(pbl, buffer);//Draw the sprite to the buffer
}
refreshScreen(buffer);//Copy the buffer to the picturebox
}
}
private void drawSprite(PictureBoxLoader pbLoader, Image _buffer)
{
using (Graphics formGraphics = Graphics.FromImage(_buffer))
{
Point imgPoint = new Point(pbLoader.imgX, pbLoader.imgY);
formGraphics.DrawImageUnscaled(pbLoader.imgImage, imgPoint);
}
}
private void refreshScreen(Image _image)
{
pictureBox_BKG.Image = _image;
this.Refresh();
}
有什么更好的方法来做到这一点的建议吗?
我尝试提前静态创建图像缓冲区,然后重新绘制背景。这有帮助,但它仍然比使用图片框慢得多。虽然,不可否认,上述方法允许适当的透明度。