Home > Blockchain >  Why when looping in timer tick event over images list it's throwing exception parameter is not
Why when looping in timer tick event over images list it's throwing exception parameter is not

Time:10-09

System.ArgumentException: 'Parameter is not valid.'

private void timer1_Tick(object sender, EventArgs e)
        { 
            for (int i = 0; i < myGifList.Count; i  )
            {
                Bitmap bmp = new Bitmap(myGifList[i]);
                pictureBox1.Image = bmp;
                bmp.Dispose();
            }
        }

After the loop end it's throwing the exception.

Before that I did :

private void timer1_Tick(object sender, EventArgs e)
        { 
            for (int i = 0; i < myGifList.Count; i  )
            {
                pictureBox1.Image = new Bitmap(myGifList[i]);
            }
        }

But then it's throwing out of memory exception.

And before all that the first time I tried this :

int counter = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(counter == 10)
            {
                counter = 0;
            }

            for (int i = 0; i < myGifList.Count; i  )
            {
                Bitmap bmp = new Bitmap(myGifList[counter]);
                pictureBox1.Image = bmp;
            }

            counter  ;
        }

This is working the images are looping in the pictureBox1 but after some time it's throwing the out of memory exception.

The full code :

I'm downloading images then reading the images back to a List then trying to display them in a picutreBox1 and using trackBar1 to change the looping speed of the images in the pictureBox1 with a timer.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Extract
{
    public partial class Form1 : Form
    {
        List<string> myGifList = new List<string>();

        public Form1()
        {            
            List<string> times = new List<string>();
            string t = "";

            InitializeComponent();

            using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
            {
                client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36");
                client.DownloadFile("https://myimages/", @"D:\localfile.html");

                var file = File.ReadAllText(@"D:\localfile.html");

                int idx = file.IndexOf("arrayImageTimes.push");
                int idx1 = file.IndexOf("</script>", idx);

                string results = file.Substring(idx, idx1 - idx);

                var statements = results.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < statements.Length; i  )
                {
                    if (i == 10)
                    {
                        break;
                    }

                    string number = statements[i].Split('\'')[1];

                    times.Add(number); // add to a list instead

                    var link = "https://myimages"   number;
                    client.DownloadFile(link, @"D:\Images\Image"   i   ".jpeg");
                }
            }

            FileInfo[] fi;
            DirectoryInfo dir1 = new DirectoryInfo(@"D:\Images");
            fi = dir1.GetFiles("*.jpeg");
            for (int i = 0; i < fi.Length; i  )
            {
                myGifList.Add(fi[i].FullName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        int counter = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(counter == 10)
            {
                counter = 0;
            }

            for (int i = 0; i < myGifList.Count; i  )
            {
                Bitmap bmp = new Bitmap(myGifList[counter]);
                pictureBox1.Image = bmp;
            }

            counter  ;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            timer1.Interval = 100 * trackBar1.Value;
        }
    }
}

CodePudding user response:

You seem like you're trying to loop over myGifList, but then you've hardcoded a value of 10 in the method.

However, the bigger issue is that you're not even using i defined by the loop, so you're just overriding the same Bitmap myGifList.Count times, for nothing - making your code inefficient.

Try:

private void timer1_Tick(object sender, EventArgs e)
{
   if (counter == myGifList.Count)
      counter = 0;

   pictureBox1.Image?.Dispose();

   Bitmap bmp = new Bitmap(myGifList[counter]);
   pictureBox1.Image = bmp;

   counter  ;
}

Note that you should use the null conditional operator ? to prevent a possible NullReferenceException when calling .Dispose():

pictureBox1.Image?.Dispose();

This would happen on the first time setting the image as pictureBox1.Image would be null, causing .Dispose() to throw an exception.


Even better would be to use the PictureBox.ImageLocation property which would allow you to display the image without creating a Bitmap object every time:

private void timer1_Tick(object sender, EventArgs e)
{
   if (counter == myGifList?.Count)
      counter = 0;

   pictureBox1.ImageLocation = myGifList[counter];

   counter  ;
}

Sidenote: the Timer.Interval property is also in milliseconds, so I'd bump up the 100 value you have in trackBar1_Scroll(...) if you want to actually be able to see the images looping.

  • Related