title: Loop through folder of images and show images one by one
body: <p>I'm new to c# and WPF and trying to build an application that loops through a folder and show's the images inside one by one. When the last one is shown it needs to show the first one again. </p>
  
  <p>I tried naming all the files 1.jpg , 2.jpg and so on, and then just loop through the amount of pictures. But if I delete one then it gives an error.</p>
  
  <p>Is there a better way to acomplish this ?</p>
  
  <p>I'm using C# and a WPF window with an image inside a grid.</p>
  
  <p>Any help would be greatly appriciated!</p>
  
  <p>edit: current code</p>
  
  <pre><code>private string[] files;
      private System.Timers.Timer timer;
  
      private int counter;
      private int Imagecounter;
  
      public IntroScreen()
      {
          InitializeComponent();
          this.Loaded += new RoutedEventHandler(this.MainWindow_Loaded);
      }
  
      private void MainWindow_Loaded(object sender, RoutedEventArgs e)
      {
  
          setupPics();
      }
  
  
  
      private void setupPics() 
      {
          timer = new System.Timers.Timer();
          timer.Elapsed += new ElapsedEventHandler(timer_Tick);
          timer.Interval = (2000);
          timer.Enabled = true;
          timer.Start();
  
          files = Directory.GetFiles("../../Resources/Taken/", "*.jpg", SearchOption.TopDirectoryOnly);
          Imagecounter = files.Length;
          counter = 0;
      }
  
      private void timer_Tick(object sender, EventArgs e)
      {
          counter++;
  
         Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() =&gt;
              {
                  Picture.Source = new BitmapImage(new Uri(files[counter - 1], UriKind.Relative));
              }));
  
         if (counter == Imagecounter)
         {
             counter = 0;
         }
  
      }
  </code></pre>
  
  <p>This isen't working, It finds the items in the folder, but the image doesn't change.</p>
  
  <p>It doesn't return an error, the images just don't show up.</p>
  
  <p>Anyone have any suggestions ?</p>