title: A generic error occurred in GDI+ ExternalException Was caught cant figure out why
body: <p>The error is on the line: </p>
  
  <pre><code>for (int x = 0; x &lt; myList.Count(); x++)
  </code></pre>
  
  <p>The <code>x++</code>is painted with green.</p>
  
  <p>Im using backgroundoworker and I used this code same code in another form before without a backgroundworker and it worked good. Now in the other form im using a click button event to <code>show()</code> this form and I want to use a <code>progressBar1</code> to show the progress of the backgroundowrker work.</p>
  
  <p>I used now try and catch inside this for loop and it went to the catch point and showed me the error. The full exception message is:</p>
  
  <pre><code> 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. 
  </code></pre>
  
  <p>This is the full code of this form:</p>
  
  <pre><code>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&lt;string&gt; 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 &gt;&gt;&gt;   " + err);
              }
          }
  
          private void backGroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              progressBar1.Value = e.ProgressPercentage;
          }
  
          private void backGroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              List&lt;string&gt; 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&lt;string&gt;();
              myGifList = new List&lt;string&gt;();
              for (int i = mtpStart; i &lt; mtpEnd; i++)
              {
                  myList.Add(fi[i].FullName);
              }
              for (int x = 0; x &lt; 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&lt;string&gt;();
              dir1 = new DirectoryInfo(tempRadarPngToGifDirectory);
              fi = dir1.GetFiles("*.gif");
              nameOfStartFile = fi[0].Name;
              for (int i = 0; i &lt; 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);
          }
      }
  }
  </code></pre>