C#加载大量图像到picturebox但占用太多内存

发布于 2025-01-13 13:53:35 字数 1553 浏览 6 评论 0原文

这是我的代码,用于将 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();
   }
}

我的问题是:

  1. 有什么方法可以减少内存使用吗?
  2. 有什么方法可以改进我的代码,使其运行得更快吗?
  3. 为什么显示 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:

  1. Is there any way to reduce the memory used?
  2. Is there any way to improve my code so that it can work faster?
  3. Why it takes that much memory to show the PictureBox?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城之時有危險 2025-01-20 13:53:35

我对创建图像缩略图做了一些修改,该程序运行速度更快并且占用的内存更少。

public bool ThumbnailCallback()
{
   return false;
}
if (ReaderTopic4.HasRows)
{
   long len = ReaderTopic4.GetBytes(0, 0, null, 0, 0);
   byte[] buffer = null;
   buffer = new byte[len];
   len = ReaderTopic4.GetBytes(0, 0, buffer, 0, (int)len);
   MemoryStream ms = new MemoryStream(buffer);
   Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
   Bitmap img = new Bitmap(ms);
   Image myThumbnail = img.GetThumbnailImage(353, 250, myCallback, IntPtr.Zero);
   pbTopic4[x].Image = myThumbnail;
 }

参考:
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.

public bool ThumbnailCallback()
{
   return false;
}
if (ReaderTopic4.HasRows)
{
   long len = ReaderTopic4.GetBytes(0, 0, null, 0, 0);
   byte[] buffer = null;
   buffer = new byte[len];
   len = ReaderTopic4.GetBytes(0, 0, buffer, 0, (int)len);
   MemoryStream ms = new MemoryStream(buffer);
   Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
   Bitmap img = new Bitmap(ms);
   Image myThumbnail = img.GetThumbnailImage(353, 250, myCallback, IntPtr.Zero);
   pbTopic4[x].Image = myThumbnail;
 }

Reference:
https://learn.microsoft.com/zh-tw/dotnet/api/system.drawing.image.getthumbnailimage?view=dotnet-plat-ext-6.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文