C#加载大量图像到picturebox但占用太多内存
这是我的代码,用于将 100 个图像从 MySQL 数据库加载到 Panel
, 每张图片大小约为 28 KB。 但资源监视器显示占用内存超过2GB, 有时它会导致内存不足异常
。
for (int x = 0; x < Topic4Number; x++)
{
ReaderKeyword.Read();
pbTopic4[x] = new PictureBox();
if (x == 0)
{
pbTopic4[x].Location = new Point(45, 75);
}
else
{
if (pbTopic4[x - 1].Right < 1300)
{
pbTopic4[x].Location = new Point(pbTopic4[x - 1].Right + 35, pbTopic4[x - 1].Top);
}
else
{
pbTopic4[x].Location = new Point(45, pbTopic4[x - 1].Top + 445);
}
}
pbTopic4[x].Size = new Size(Topic4pb_width, Topic4pb_height);
pbTopic4[x].SizeMode = PictureBoxSizeMode.Zoom;
if (ReaderKeyword.HasRows)
{
long len = ReaderKeyword.GetBytes(0, 0, null, 0, 0);
byte[] buffer = new byte[len];
len = ReaderKeyword.GetBytes(0, 0, buffer, 0, (int)len);
MemoryStream ms = new MemoryStream(buffer);
Bitmap img = new Bitmap(ms);
pbTopic4[x].Image = img;
}
}
PanelAll.Controls.AddRange(pbTopic4);
一旦不需要那些 PictureBox
,我确实使用 .Dispose()
来释放内存。
if (pbTopic4 != null)
{
for (int x = 0; x < Topic4Number; x++)
{
pbTopic4[x].Dispose();
pbTopic4[x] = null;
GC.Collect();
}
}
我的问题是:
- 有什么方法可以减少内存使用吗?
- 有什么方法可以改进我的代码,使其运行得更快吗?
- 为什么显示
PictureBox
需要那么多内存?
This is my code to load 100 images from MySQL database to a Panel
,
and each of the image is about 28 KB.
However, the resource monitor shows that it takes more than 2 GB of memory,
and sometimes it leads to out of memory exception
.
for (int x = 0; x < Topic4Number; x++)
{
ReaderKeyword.Read();
pbTopic4[x] = new PictureBox();
if (x == 0)
{
pbTopic4[x].Location = new Point(45, 75);
}
else
{
if (pbTopic4[x - 1].Right < 1300)
{
pbTopic4[x].Location = new Point(pbTopic4[x - 1].Right + 35, pbTopic4[x - 1].Top);
}
else
{
pbTopic4[x].Location = new Point(45, pbTopic4[x - 1].Top + 445);
}
}
pbTopic4[x].Size = new Size(Topic4pb_width, Topic4pb_height);
pbTopic4[x].SizeMode = PictureBoxSizeMode.Zoom;
if (ReaderKeyword.HasRows)
{
long len = ReaderKeyword.GetBytes(0, 0, null, 0, 0);
byte[] buffer = new byte[len];
len = ReaderKeyword.GetBytes(0, 0, buffer, 0, (int)len);
MemoryStream ms = new MemoryStream(buffer);
Bitmap img = new Bitmap(ms);
pbTopic4[x].Image = img;
}
}
PanelAll.Controls.AddRange(pbTopic4);
I did use .Dispose()
to free the memory once I don't need those PictureBox
.
if (pbTopic4 != null)
{
for (int x = 0; x < Topic4Number; x++)
{
pbTopic4[x].Dispose();
pbTopic4[x] = null;
GC.Collect();
}
}
Here are my questions:
- Is there any way to reduce the memory used?
- Is there any way to improve my code so that it can work faster?
- Why it takes that much memory to show the
PictureBox
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对创建图像缩略图做了一些修改,该程序运行速度更快并且占用的内存更少。
参考:
https: //learn.microsoft.com/zh-tw/dotnet/api/system.drawing.image.getthumbnailimage?view=dotnet-plat-ext-6.0
I made some modification about creating thumbnail of the images, the program works faster and takes much lesser memory.
Reference:
https://learn.microsoft.com/zh-tw/dotnet/api/system.drawing.image.getthumbnailimage?view=dotnet-plat-ext-6.0