c# - C# 表单 - 动态创建大量按钮/面板/图片框很慢。如何加快速度?
问题描述
由于我不是使用 C# 表单的专家,我希望我能指出正确的方向,因为我运行的特定代码部分运行缓慢,我不知道如何加快速度。
我现在的方式是让用户选择图像和图块大小。
根据图像的尺寸和选定的图块大小,我一直在主图像上方绘制图像。这些图像是平铺大小,带有一个填充主图像尺寸的数字。当我单击它们时,我希望它们像按钮一样。到目前为止,这些图像已为此目的制作成面板。
如果我不必在每次用户更改图像或图块大小时重新计算我需要多少个面板,那也不会那么糟糕。但实际上,它会持续大约半秒,尤其是在较小的瓷砖尺寸下。
换句话说,既然我必须制作很多图像/控件并动态显示它们,你会建议我做什么来提高效率?
到目前为止我所拥有的。
pictureBox.Controls.Clear();
//Create our large list of buttons
for (int y = 0; y < tileCountHeight; y++)
{
for (int x = 0; x < tileCountWidth; x++)
{
Panel newPicture = new Panel();
int locX = x * tileSize;
int locY = y * tileSize;
newPicture.Location = new Point(locX, locY);
//Determine the starting image
int dataIndex = x + (y * tileCountWidth);
if (passabilityList.Count >= dataIndex)
{
passabilityList.Add(Passability.Passable);
}
switch (passabilityList[dataIndex])
{
case Passability.Blocked:
newPicture.BackgroundImage = SquareImage;
break;
case Passability.HideBehind:
newPicture.BackgroundImage = TriangleImage;
break;
case Passability.Passable:
newPicture.BackgroundImage = CircleImage;
break;
}
newPicture.Width = tileSize;
newPicture.Height = tileSize;
newPicture.Visible = true;
newPicture.BackgroundImageLayout = ImageLayout.Center;
newPicture.Name = "picOptionX" + x + "Y" + y;
pictureBox.Controls.Add(newPicture);
}
}
解决方案
推荐阅读
- google-cloud-platform - pip install 后无法在 Google AI Platform Notebook 中导入 sklearn_pandas
- python - Fusioncharts 入门指南页面仍然为空
- javascript - 如何将函数实例对象与空对象进行比较
- keras - 如何在 keras 中使用特定的 GPU 进行多 GPU 训练?
- php - 如何从字符串中提取 ID 号?
- node.js - 无法在express nodejs中的查询功能之外获得结果
- php - 检查 Laravel 5.5 模型事件中的属性是否已更改
- c# - Twitter API 返回 401 Unauthorized
- html - 单击按钮时将图像放入内部作为背景按钮
- python - 从函数内部调用多个函数