Search code examples
c#.netwinformspicturebox

Load images from directory into picture box at certain intervals


Can anyone help me in loading images from directory into picture box at certain intervals. For eg: i have some images in \Picture folder like 1.jpg,2.jpg..etc. So my requirement is to loop through Picture Directory and load 1.jpg into Picture box then wait for 5 sec, and then load 2.jpg into picture box.


Solution

  • Finally got it, hope it will be helpful for others:

    private void Form_Load(object sender, EventArgs e)
            {
                moveTimer.Interval = 1000;
                moveTimer.Tick += new EventHandler(moveTimer_Tick);
                moveTimer.Start();
            }
        private void moveTimer_Tick(object sender, System.EventArgs e)
                {
                   string[] images = Directory.GetFiles(@"C:\Dir", "*.jpg");  
                   image = Image.FromFile(images[counter]);
                   pictureBox.Width = image.Width;
                   pictureBox.Height = image.Height;
                   pictureBox.Image = image;
    
    
                    // Move Image to new location
                    pictureBox.Left = rand.Next(Math.Max(0, Bounds.Width - pictureBox.Width));
                    pictureBox.Top = rand.Next(Math.Max(0, Bounds.Height - pictureBox.Height));
    
                    if (counter < images.Count - 1)
                    {
                        counter = counter + 1;
                    }
                    else
                    {
                        counter = 0;
                    }
                }