在C#中使用真正低内存显示大量图像? [英] Show a lot of images with really low memory using in C#?

查看:1121
本文介绍了在C#中使用真正低内存显示大量图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我在 Panel 中动态创建8 PictureBox es,其中我显示了8个不同的图像,每 PictureBox 一个。图像是50Kb Jpeg。
问题是我想让用户看到300张图片,就像38张面板一样,但每次 LOAD 图片进入 PictureBox 我的程序使用5-7Mb的Ram内存 MORE 。(我在Windows的任务管理器中看到它)
如何减少增量?我的图片不是那么大,我只想展示预览,我不关心质量。
以下是我如何创建 PictureBox es:

I have a form in which I dynamically create 8 PictureBoxes in a Panel in which I show 8 different images, one every PictureBox. Images are 50Kb Jpeg. The problem is that I want to allow user to see also 300 pictures,that's like 38 panels, but every time I LOAD images into PictureBoxes my program uses 5-7Mb of Ram memory MORE.( I see it from the Windows' task manager) How can i reduce that increment? My Pictures aren't that big, I only want to show a preview, i don't care about quality. Here is how i CREATE PictureBoxes:

private void buttonAdd_Click(object sender, EventArgs e)
{     
  Panel pan = new Panel();
  PictureBox pic1 = new PictureBox();
  pic1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  pic1.Location = new System.Drawing.Point(208, 5);
  pic1.Name = "panel" + ctr.Count().ToString() + "pictureBox1";
  pic1.Size = new System.Drawing.Size(100, 100);
  pic1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  ///
  /// Same thing other 7 times with differents name and location
  ///      
  pan.Location = new System.Drawing.Point(12, 55);
  pan.Name = "panel" + ctr.Count().ToString();
  pan.Size = new System.Drawing.Size(1088, 129);
  pan.TabIndex = 0;
  pan.AllowDrop = true;
  pan.BackColor = System.Drawing.SystemColors.ControlLight;
  pan.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;           
  pan.Controls.Add(pic1);
  ///
  /// Same thing other 7 times 
  ///        
  this.Controls.Add(pan);
  }

以下是我填写的方式:

void buttonClickEvent(object sender, EventArgs e)
    {
        string[] files= Directory.GetFiles("mylocalpath");
        foreach(string x in files)
        {
            string imgIndex= x.Remove(0,x.LastIndexOf("_")+1).Remove(1);
            PictureBox pic=pictureBox1; 
            //I get it everytime from imgIndex and sender's parent,
            //too much code to show now
            pic.ImageLocation = x;
        }

    }


推荐答案

您可以在很长一段时间内忽略.NET中的IDisposable接口。许多.NET程序员从不在一次性对象上调用Dispose()方法,也从不使用使用语句,并且他们的程序工作得很好。垃圾收集器使程序保持一致,调用这些类上的终结器进行清理。

You can ignore the IDisposable interface in .NET for a very long time. Many .NET programmers never call the Dispose() method on disposable objects and never use the using statement and their programs work just fine. The garbage collector keeps the program humming along, calling the finalizer on such classes to clean up.

但Image和Bitmap类是特殊的。它们是围绕GDI +的非常瘦的托管类包装器,一个不受管理的api。图像可以使用 lot 的非托管内存来存储像素数据,在大图像上存储许多兆字节。将它与一个没有分配足够的托管对象以触发垃圾收集的程序相结合,你就会有一个胖小猪问题。

But the Image and Bitmap classes are special. They are very thin managed classes wrappers around GDI+, an unmanaged api. Images can use lots of unmanaged memory to store the pixel data, many megabytes on large images. Combine that with a program that doesn't otherwise allocate enough managed objects to trigger a garbage collection and you've got a fat piggy problem.

必须在使用完图像后处理图像以避免此问题。使用PictureBox.ImageLocation属性时,这有点困难,PictureBox类使用工作线程加载图像。它本身不能处理前一个图像,它无法知道图像是否可能在其他地方使用。你必须帮忙:

You must dispose images after you are done using them to avoid this problem. That's a bit difficult to do when you use the PictureBox.ImageLocation property, the PictureBox class loads the image using a worker thread. It cannot itself dispose the previous image, it cannot know if the image might be used somewhere else. You have to help:

    foreach(string x in files)
    {
        string imgIndex = x.Remove(0,x.LastIndexOf("_")+1).Remove(1);
        PictureBox pic = pictureBox1; 
        //...
        if (pic.Image != null) pic.Image.Dispose();
        pic.Image = null;
        pic.ImageLocation = x;
    }

或者使用Image.FromFile()方法,所以你没有看看空白的图片框。

Or use the Image.FromFile() method instead so you don't have to look at the empty picture box.

这篇关于在C#中使用真正低内存显示大量图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆