gpt4 book ai didi

C# picturebox的加载时间

转载 作者:行者123 更新时间:2023-11-30 22:24:43 24 4
gpt4 key购买 nike

游戏画面背景包含大量的图片框,启动、最小化和移动后需要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);
}
}

最佳答案

对于这种项目,恐怕你能做的不多。 Visual C# 并非真正为游戏而设计,但有几种方法可以加快速度。

1.) 如果你的游戏不需要改变背景,首先加载它并且只重置你需要的图片(可以在标签值中使用一个 bool 值来决定你是否应该重绘。)

2.) 使用 threading .如果您启动 5 个线程,您的渲染可能会加速大约 3 倍。

3.) 在构造函数中将您的三个图像转换为 Image 对象,这样您就不需要为每个图 block 转换它们。

关于C# picturebox的加载时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12649774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com