0

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!

user2921015
  • 69
  • 1
  • 9
  • 2
    Remove the loop. You need to use the `Timer.Tick` event to change the Image (the `Tick` event belongs to the `System.Windows.Forms.Timer` component). – Jimi Apr 19 '20 at 22:27

1 Answers1

1

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

Tomas Paul
  • 490
  • 5
  • 13
  • 1
    `pictureBox1.Image?.Dispose(); pictureBox1.Image = Image.FromFile(imagePath);` – Jimi Apr 19 '20 at 22:46
  • I modified my code like your example above, except instead of enabling the timer with a button, I'm enabling it in the form load method as I need it to start when the form loads.The form runs but no pictures load. – user2921015 Apr 20 '20 at 00:40
  • pictureTimer.Tick += new EventHandler(pictureTimer_Tick); pictureTimer.Interval = 5000; _images = Directory.GetFiles(_imagesFolder); pictureTimer.Enabled = true; – user2921015 Apr 20 '20 at 00:41
  • Hi @user2921015, I moved enabling the timer to the `Form1_Load()` method and it is working. When you put breakpoint onto `pictureTimer_Tick()`, is your timer ticking? Does `_images` array contain any elements? Is `pictureBox1.Visible` set to true? – Tomas Paul Apr 20 '20 at 06:43
  • I had everything visible, not sure why it didn't work. I deleted my form and redid it and it works now! I'll mark as answered, but one more question. How do I fit the image proportionately? My images are not all the same proportion, so if I use the StretchImage property, sometimes the image is skewed. I would like it to fit the size of the picturebox but keep it's proportions, so if the picture box is square and the image is taller than it is wide, it shows in the picture box taller than it is wide. Thoughts? – user2921015 Apr 20 '20 at 11:45
  • 1
    Nevermind. I just load the same image into an image object and resize the picturebox to that image width/height. Thanks again! – user2921015 Apr 20 '20 at 12:02