内存不足,在一个图片框中有多个图像 [英] Out of memory with multi images in one picturebox

查看:121
本文介绍了内存不足,在一个图片框中有多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将一些图像加载到一个图片框中时,我遇到了内存不足的问题。

I have a problem with out of memory when I'm trying load a few images into one picturebox.

public void button2_Click(object sender, EventArgs e)
    {


        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.ShowDialog();
        string selected = dialog.SelectedPath;

        string[] imageFileList = Directory.GetFiles(selected);


        int iCtr = 0,zCtr = 0;
        foreach(string imageFile in imageFileList)
        {

            if (Image.FromFile(imageFile) != null)
            {
                Image.FromFile(imageFile).Dispose();
            }

            PictureBox eachPictureBox = new PictureBox();

            eachPictureBox.Size = new Size(100,100);
           // if (iCtr % 8 == 0)
            //{
             //   zCtr++;
              //  iCtr = 0;
            //}
            eachPictureBox.Location = new Point(iCtr * 100 + 1, 1);
            eachPictureBox.Image = Image.FromFile(imageFile);
            iCtr++;

            panel1.Controls.Add(eachPictureBox);

        }


    }`enter code here`


推荐答案


if (Image.FromFile(imageFile) != null)
{
    Image.FromFile(imageFile).Dispose();
}


错误。您正在从文件加载图像,检查结果是否为null ...然后再次将其加载到新结果中以便您可以处置它。虽然后一部分是愚蠢的,但它没有害处。但是,第一部分是因为结果 Image 从未正确处理(如果/当GC收集它时, Image <上的终结器输入应该处理非托管资源,但这不是一件明智的事情。)

Bad. You're loading the image from the file, checking to see if the result is null...then loading it again into a new result so that you can dispose it. While the latter portion is silly, it isn't harmful. The first portion is, however, as the resulting Image is never properly disposed of (if/when the GC collects it, the finalizer on the Image type should dispose of the unmanaged resources, but this is not a wise thing to rely on).

顺便说一下, Image.FromFile 永远不会返回 null 。如果它无法读取图像,那么它将抛出 OutOfMemoryException

Incidentally, Image.FromFile will never return null. If it cannot read the image, then it will throw an OutOfMemoryException.

代码似乎也什么都不做,因为没有 else 块,在 if 块中没有任何意义。

The code also appears to do nothing, since there's no else block and nothing meaningful is done in the if block.

我的猜测是你的 OutOfMemoryException 来自于该目录中的一个或多个文件存储在损坏或不受支持的事实中图像格式,或者根本不是图像。

My guess is that your OutOfMemoryException is coming from the fact that one or more of the files in that directory is stored in a corrupted or unsupported image format, or isn't an image at all.

尝试替换 foreach中的代码用这个:

try
{
    Image image = Image.FromFile(imageFile);

    PictureBox eachPictureBox = new PictureBox();

    eachPictureBox.Size = new Size(100,100);

    eachPictureBox.Location = new Point(iCtr * 100 + 1, 1);
    eachPictureBox.Image = Image.FromFile(imageFile);
    iCtr++;

    panel1.Controls.Add(eachPictureBox);
}
catch(OutOfMemoryException) { } // skip the file

这篇关于内存不足,在一个图片框中有多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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