2011-05-27 95 views
1

我有一个创建一个GIF动画功能,它总是完美的工作,但现在当我所有的GIF图像是黑白的,它提供了一个内存溢出的例外论:内存异常

e.AddFrame(图像。 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; 
    } 
+0

感谢您的回答。注意:这不是因为黑白GIF,我正在查看错误的文件夹并尝试添加.txt文件> _> – 2011-05-27 20:00:01

回答

3

Image.FromFile的文档说,如果文件不包含有效图像,或者图像格式为GDI +不支持的格式,它将抛出OutOfMemoryException

,如果你重写你的代码会发生什么:

for (int i = 0, count = imageFilePaths.Length; i < count; i++) 
{ 
    var img = Image.FromFile(imageFilePaths[i]); 
    e.AddFrame(img); 
} 

如果你在调用Image.FromFile例外,那是因为你的图像无法加载。

+0

是的,OOM是一般错误。乐趣。 – 2011-05-27 19:48:51

+0

谢谢,我会找到另一种方式来加载它。说,你怎么知道的?你只是检查了我所有的OOM异常函数,或者你碰巧知道这个问题? – 2011-05-27 19:55:56

+0

@或Betzalel:我不必检查所有的功能。你说它死在那条线上,所以只有两种可能性。调试的基本规则是将事情分解成最简单的步骤,所以我首先看到的是Image.FromFile是否可以抛出OutOfMemoryException异常。 – 2011-05-27 19:59:33

0

我不知道的细节,但这看起来并不写。没有'使用',所以你可能没有处理资源。