2011-12-12 64 views
0

的錯誤就行了:一般性錯誤+ ExternalException當時陷入無法弄清楚,爲什麼

for (int x = 0; x < myList.Count(); x++) 

x++被塗成綠色。

進出口使用backgroundoworker,我用另一種形式的代碼相同的代碼之前,沒有一個BackgroundWorker和它的工作好。現在在另一個表單中,我使用一個點擊按鈕事件show()這個表單,我想用一個progressBar1來顯示backgroundowrker工作的進度。

我現在使用的嘗試,趕上這裏面for循環和它去到交點,給我的錯誤。完整的異常消息是:

at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) 
at System.Drawing.Image.Save(String filename, ImageFormat format) 
at mws.Animation_Radar_Preview.backGroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Animation_Radar_Preview.cs:line 163 

gifImages isnt null. 

這是這種形式的全碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using DannyGeneral; 
using unfreez_wrapper; 


namespace mws 
{ 
    public partial class Animation_Radar_Preview : Form 
    { 
     int mtpStart; 
     int mtpEnd; 
     Picturebox1_Fullscreen pb1; 
     string radar_images_download_directory; 
     string tempRadarPngToGifDirectory; 
     int numberOfFiles; 
     UnFreezWrapper unfreez; 
     string path_exe; 
     List<string> myList; 
     string previewDirectory; 
     int animatedGifSpeed; 
     bool loop; 
     string nameOfStartFile; 
     string nameOfEndFile; 
     string previewFileName; 
     BackgroundWorker backGroundWorker1; 
     Image img; 
     private MemoryStream _memSt = null; 
     public Animation_Radar_Preview() 
     { 
      InitializeComponent(); 
      mtpStart = Picturebox1_Fullscreen.mtp1Start; 
      mtpEnd = Picturebox1_Fullscreen.mtp1End; 
      animatedGifSpeed = Picturebox1_Fullscreen.animatedSpeed; 
      loop = Picturebox1_Fullscreen.looping; 
      pb1 = new Picturebox1_Fullscreen(); 
      radar_images_download_directory = Options_DB.Get_Radar_Images_Download_Directory(); 
      path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath); 
      tempRadarPngToGifDirectory = path_exe + "\\" + "tempRadarPngToGifDirectory"; 
      if (Directory.Exists(tempRadarPngToGifDirectory)) 
      { 
      } 
      else 
      { 
       Directory.CreateDirectory(tempRadarPngToGifDirectory); 
      } 
      previewDirectory = path_exe + "\\" + "previewDirectory"; 
      if (Directory.Exists(previewDirectory)) 
      { 
      } 
      else 
      { 
       Directory.CreateDirectory(previewDirectory); 
      } 
      previewFileName = previewDirectory + "\\" + "preview.gif"; 
      loop = false; 
      animatedGifSpeed = 0; 
      unfreez = new UnFreezWrapper(); 
      backGroundWorker1 = new BackgroundWorker(); 
      backGroundWorker1.WorkerSupportsCancellation = true; 
      this.backGroundWorker1.WorkerReportsProgress = true; 
      backGroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backGroundWorker1_ProgressChanged); 
      backGroundWorker1.DoWork += new DoWorkEventHandler(backGroundWorker1_DoWork); 
      backGroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backGroundWorker1_RunWorkerCompleted); 
      backGroundWorker1.RunWorkerAsync(); 
      progressBar1.Value = 0; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      DialogResult result1; 
      result1 = new DialogResult(); 
      SaveFileDialog sd = new SaveFileDialog(); 
      sd.Title = "Select a folder to save the animated gif to"; 
      sd.InitialDirectory = "c:\\"; 
      sd.FileName = null; 
      sd.Filter = "Gif File|*.gif;*.jpg|Gif|*.gif"; 
      sd.FilterIndex = 1; 
      sd.RestoreDirectory = true; 
      result1 = sd.ShowDialog(); 
      string file1 = sd.FileName; 
      if (result1 == DialogResult.OK) 
      { 
       File.Move(previewFileName, file1); 
      } 
     } 

     public void pictureBoxImage(string pbImage) 
     { 
      Image img2 = null; 
      try 
      { 
       using (img = Image.FromFile(pbImage)) 
       { 
        //get the old image thats loaded from the _memSt memorystream 
        //and dispose it 
        Image i = this.pictureBox1.Image; 
        this.pictureBox1.Image = null; 

        if (i != null) 
         i.Dispose(); 

        //grab the old stream 
        MemoryStream m = _memSt; 

        //save the new image to this stream 
        _memSt = new MemoryStream(); 
        img.Save(_memSt, System.Drawing.Imaging.ImageFormat.Gif); 

        if (m != null) 
         m.Dispose(); 

        //create our image to display 
        img2 = Image.FromStream(_memSt); 
       } 

       if (img2 != null) 
        pictureBox1.Image = img2; 
       label2.Text = numberOfFiles.ToString(); 
       label6.Text = nameOfStartFile.ToString(); 
       label4.Text = nameOfEndFile.ToString(); 
       //File.Delete(pbImage); 
      } 
      catch(Exception err) 
      { 
       Logger.Write("Animation Error >>> " + err); 
      } 
     } 

     private void backGroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      progressBar1.Value = e.ProgressPercentage; 
     } 

     private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      List<string> myGifList; 
      Image gifImages = null; 
      //button1.Enabled = false; 
      Animation_Radar_Preview ap = new Animation_Radar_Preview(); 
      //ap.FormClosing += new FormClosingEventHandler(ap_FormClosing); 
      FileInfo[] fi; 
      DirectoryInfo dir1 = new DirectoryInfo(radar_images_download_directory); 
      fi = dir1.GetFiles("*.png"); 
      myList = new List<string>(); 
      myGifList = new List<string>(); 
      for (int i = mtpStart; i < mtpEnd; i++) 
      { 
       myList.Add(fi[i].FullName); 
      } 
      for (int x = 0; x < myList.Count(); x++) 
      { 
       try 
       { 
        gifImages = Image.FromFile(myList[x]); 
        gifImages.Save(tempRadarPngToGifDirectory + "\\" + x.ToString("D6") + ".Gif", System.Drawing.Imaging.ImageFormat.Gif); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(x.ToString() + "\r\n" + (gifImages == null).ToString() + "\r\n" + ex.Message); 
       } 
      } 

      myGifList = new List<string>(); 
      dir1 = new DirectoryInfo(tempRadarPngToGifDirectory); 
      fi = dir1.GetFiles("*.gif"); 
      nameOfStartFile = fi[0].Name; 
      for (int i = 0; i < fi.Length; i++) 
      { 
       myGifList.Add(fi[i].FullName); 
       //backGroundWorker1.ReportProgress(i); 
       nameOfEndFile = fi[i].Name.Length.ToString(); 
      } 
      numberOfFiles = myGifList.Count(); 
      unfreez.MakeGIF(myGifList, previewFileName, animatedGifSpeed, loop); 
      /*this.Invoke((MethodInvoker)delegate 
      { 
       pictureBoxImage(previewFileName); 
      });*/ 
     } 

     private void backGroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      pictureBoxImage(previewFileName); 
     } 
    } 
} 
+1

你忘gifImages.Dispose()保存()調用之後。需要釋放文件鎖定。 –

回答

1

我不能完全確定什麼錯誤,尤其是你說的這個相同的代碼工作過 - 但不清楚它是否使用同一組圖像。

總之,the documentationSave法說,一個ExternalException將在兩種情況之一拋出:

  1. 圖像保存格式不正確。
  2. 圖像被保存到它創建的文件。

由於您要更改擴展名,所以它不能保存在文件上,因此它必須是錯誤的格式。這實際上意味着什麼不清楚。也許源圖像之一是使用不能轉換爲GIF的PNG格式的功能,例如, alpha通道透明度。

如果我可以花一點時間做一些其他的建議太:

if (!Directory.Exists(tempRadarPngToGifDirectory) 
{ 
    Directory.CreateDirectory(tempRadarPngToGifDirectory); 
} 

代碼操縱文件:

if (Directory.Exists(tempRadarPngToGifDirectory)) 
{ 
} 
else 
{ 
    Directory.CreateDirectory(tempRadarPngToGifDirectory); 
} 

如果顛倒邏輯空「成功」的情況下,不需要路徑如下:

tempRadarPngToGifDirectory = path_exe + "\\" + "tempRadarPngToGifDirectory"; 

您應該考慮使用Path類,因爲這將處理邊界情況更好,讓更多的可移植代碼:

tempRadarPngToGifDirectory = Path.Combine(path_exe, "tempRadarPngToGifDirectory"); 
+0

同意 - 當您嘗試將圖像保存爲無法保存爲圖像的文件格式時,會出現此錯誤。 – dash

+0

我無法將png轉換爲gif?或者,也許我需要以另一種方式來做,然後保存? –

+0

我在暗示可能特定的PNG文件使用.NET Image類無法轉換的一些高級功能,例如alpha通道透明度或動畫等。你看過它看起來有沒有什麼異常的圖像嗎? –

相關問題