游戏画面背景包含大量图片框,启动、最小化、移动后需要2-3秒才能完全显示背景。这段代码对我的目的有害吗?还有另一种方法可以做到这一点吗?谢谢你。这是代码:
public class gameArea
{
private Panel gameArea;
private MemoryStream ms;
public gameArea(Panel gameArea)
{
this.gameArea = gameArea;
}
public void setBackground()
{
byte[] b_grass = File.ReadAllBytes(ImageFile.grassBG);
byte[] b_tree = File.ReadAllBytes(ImageFile.tree);
byte[] b_sea = File.ReadAllBytes(ImageFile.sea);
// [650/50=13, 550/50=11]
int[,] tile_id = {
{ 1, 2, 0, 0, 0, 1, 1, 0, 1, 2, 1, 2, 0 },
{ 2, 1, 0, 1, 0, 1, 1, 2, 2, 2, 1, 2, 0 },
{ 1, 2, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1 },
{ 1, 2, 0, 0, 1, 0, 1, 0, 1, 2, 0, 0, 0 },
{ 1, 0, 2, 0, 0, 1, 1, 2, 1, 2, 1, 2, 0 },
{ 2, 2, 0, 1, 0, 1, 2, 1, 0, 2, 0, 1, 1 },
{ 1, 1, 2, 0, 0, 0, 1, 2, 1, 1, 2, 0, 0 },
{ 0, 2, 0, 2, 1, 2, 1, 0, 1, 2, 1, 2, 2 },
{ 1, 2, 0, 0, 0, 1, 0, 2, 0, 0, 1, 2, 0 },
{ 0, 2, 0, 1, 0, 1, 1, 2, 1, 2, 0, 0, 2 },
{ 1, 1, 1, 0, 0, 0, 1, 2, 1, 2, 1, 2, 0 }
};
// tree id = 1
// sea id = 2
this.ms = new MemoryStream(b_grass);
this.gameArea.BackgroundImage = Image.FromStream(ms);
this.gameArea.BackColor = System.Drawing.Color.Transparent;
for (int yIndex = 0, y = 0; y < this.gameArea.Height; y += 50, yIndex++)
{
for (int xIndex = 0, x = 0; x < this.gameArea.Width; x += 50, xIndex++)
{
switch (tile_id[yIndex, xIndex])
{
case 1:
{
setTile(b_tree, x, y);
break;
}
case 2:
{
setTile(b_sea, x, y);
break;
}
default:
{
break;
}
}
}
}
}
private void setTile(byte[] b_img, int x, int y)
{
this.ms = new MemoryStream(b_img);
PictureBox pic = new PictureBox();
pic.Image = Image.FromStream(ms);
pic.Size = new Size(50, 50);
pic.Location = new Point(x, y);
this.gameArea.Controls.Add(pic);
}
}