Search code examples
c#visual-studio-2019picturebox

Issue with Picturebox and trying to update images


I am trying to update an image in a picture box on a timer. I'll be honest, I don't have much experience with pictureboxes and it's been about 5 years since I've done any C# work in general. I tried to search on google and on here and can't seem to find the answer that I think is what I need :/

Essentially, this is what I am trying to do. A form takes in a time in seconds (say, 5 seconds), and when a button is clicked a new form is opened. This new form has a picture box on it that will display a random photo from a directory. I am able to get the picture box to show a random photo, but when I try to get it to refresh without reopening the form, this is where I am having an issue.

I have a variable (timeVar) set to an inputted time in seconds, and a timer in the background where every time the timer ticks, it updates timerVar by subtracting 1. This is what i have to set the picture in the picture box for each iteration. There is an outerloop that loops through my list (dirList) until it has hit every item in the list.

    while(timerVar > 0)
{
     pictureTimer.Start();
     imagePathPic = imagePath + dirList[ind];
     sessionPicture.ImageLocation = @imagePathPic;
     sessionPicture.Refresh();
}
    pictureTimer.Stop();
    timeVar = 5;

dirList is a list of all images inside the given diretory, and imagePath is a string that holds the directory. The functionality of my list/string was tested successfully, as well as the outer loop, but when I apply the picturebox in the loop above, it doesn't do anything until it gets the the last picture then it displays. What am I missing to get it to display each picture on the form? Please let me know if you need any more information.

Thank you!


Solution

  • For me it works to load the image from file. Image.FromFile() is under System.Drawing namespace. I did not have to add any Refresh() method. You can try to call Refresh on your user form this.Refresh(), or just simply call Refresh().

    public partial class Form1 : Form
    {
        Timer _timer = new Timer();
        string[] _images;
        Random _random = new Random();
        string _imagesFolder = @"C:\Users\Me\Desktop\Picures\";
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            _timer.Tick += new EventHandler(timer_Tick);
            _timer.Interval = 2000;
            _images = Directory.GetFiles(_imagesFolder);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            _timer.Enabled = !_timer.Enabled;
        }
    
        private void timer_Tick(object sender, EventArgs e)
        {
            var index = _random.Next(0, _images.Length);
            var imagePath = Path.Combine(_imagesFolder, _images[index]);
            pictureBox1.Image = Image.FromFile(imagePath);
        }
    }
    

    Here is the mockup:

    mockup