Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:11+00:00 2026-05-27T11:03:11+00:00

The error is on the line: for (int x = 0; x < myList.Count();

  • 0

The error is on the line:

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

The x++is painted with green.

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 show() this form and I want to use a progressBar1 to show the progress of the backgroundowrker work.

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:

 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. 

This is the full code of this form:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:03:12+00:00Added an answer on May 27, 2026 at 11:03 am

    I’m not entirely sure what is going wrong, especially as you say this same code worked previously — but it is not clear whether it worked with the same set of images.

    Anyhow, the documentation for the Save method says that an ExternalException will be thrown in one of two situations:

    1. Image was saved with the wrong format.
    2. Image is saved to the file it was created from.

    It cannot be that you are saving over the file because you are changing its extension, so it must be the wrong format. What this actually means is not clear. Perhaps one of the source images is using capabilities of the PNG format that cannot be converted to GIF, e.g. alpha channel transparency.

    If I may take a moment to make some other suggestions too:

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

    The empty ‘success’ case is not required if you invert the logic:

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

    Code that manipulates file paths such as the following:

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

    You should consider using the Path class as this will handle the edge cases better and make more portable code:

    tempRadarPngToGifDirectory = Path.Combine(path_exe, "tempRadarPngToGifDirectory");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've singled out a runtime error on this line of my code: for (synsAuxCopyIndex=1;
I keep getting an expected identifier error at this line in my code =/
I'm getting the following error on the line of code below: Syntax error (missing
JSLint is giving me this error: Problem at line 11 character 33: Use the
I'm getting a compiler error for this line: Collections.sort(terms, new QuerySorter_TFmaxIDF(myInteger)); My customized Comparator
I'm getting an unexpected T_CONCAT_EQUAL error on a line of the following form: $arg1
The line where this error was called from is this: $celkova_suma=mysql_result($c_res, 0, 0)+mysql_result($d_res, 0,
i am using following code to setlistview adapter but giving me error at last
In my program on this line: int value = MTEConnect(auth_string, err); I receive such
This is the code that I've written. int num; try { num=100; DoSomething(); System.out.println(num);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.