2011-05-27 91 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

我不知道的細節,但這看起來並不寫。沒有'使用',所以你可能沒有處理資源。