内存不足异常

问题描述

| 我具有创建动画gif的功能,它始终可以完美运行,但是现在当我所有的gif均为黑白时,它会显示OutOfMemory Exception On:   e.AddFrame(Image.FromFile(imageFilePaths [i])); 我的职能:
public void MakeGif(string sourcefolder,string destinationgif)
    {
        Isgifing = true;
        string path = MainForm.RootDirectory;
        String[] imageFilePaths = Directory.GetFiles(path);
        String outputFilePath = MainForm.RootDirectory + @\"\\Final.gif\";
        AnimatedGifEncoder e = new AnimatedGifEncoder();
        e.Start(outputFilePath);
        e.SetDelay(300);
        //-1:no repeat,0:always repeat

        e.SetRepeat(0);
        for (int i = 0,count = imageFilePaths.Length; i < count; i++)
            e.AddFrame(Image.FromFile(imageFilePaths[i]));
        e.Finish();
        Isgifing = false;
    }
AddFrame函数
public bool AddFrame(Image im) 
    {
        if ((im == null) || !started) 
        {
            return false;
        }
        bool ok = true;
        try 
        {
            if (!sizeSet) 
            {
                // use first frame\'s size
                SetSize(im.Width,im.Height);
            }
            image = im;
            GetimagePixels(); // convert to correct format if necessary
            AnalyzePixels(); // build color table & map pixels
            if (firstFrame) 
            {
                WriteLSD(); // logical screen descriptior
                WritePalette(); // global color table
                if (repeat >= 0) 
                {
                    // use NS app extension to indicate reps
                    WritenetscapeExt();
                }
            }
            WriteGraphicCtrlExt(); // write graphic control extension
            WriteImageDesc(); // image descriptor
            if (!firstFrame) 
            {
                WritePalette(); // local color table
            }
            WritePixels(); // encode and write pixel data
            firstFrame = false;
        } 
        catch (IOException e) 
        {
            ok = false;
        }

        return ok;
    }
    

解决方法

        Image.FromFile的文档说,如果文件不包含有效的图像,或者该图像采用GDI +不支持的格式,它将抛出“ 2”。 如果将代码重写为:
for (int i = 0,count = imageFilePaths.Length; i < count; i++)
{
    var img = Image.FromFile(imageFilePaths[i]);
    e.AddFrame(img);
}
如果调用
Image.FromFile
时出现异常,那是因为无法加载图像。     ,        我不知道详细信息,但是看起来并不写。没有“用法”,因此您可能不会处理资源。