user2462086
user2462086

Reputation: 23

Displaying all the images in a folder in image control with 5sec time gap

I have a folder with 1000 images that have the image name in a sequence "ICON000,ICON001 till ICON 999". I need them to be displayed in my WPF image control sequentially with 5sec time delay. i have used a file dialogue box to get the path of the particular folder and the corresponding prefix of the image (ICON). i used the below code

string path = null;
string selected_file;
string URI;`enter code here`
openfile.AddExtension = true;
openfile.Filter = "GIF(*.gif)|*.gif|PNG(*.png)|*.png|JPG(*.jpg)|*.jpg|JPEG(*.jpeg)|*.jpeg";
DialogResult result = openfile.ShowDialog();
if (result.ToString() == "OK")
{
selected_file = openfile.FileName;
 Uri uri_temp = new Uri(selected_file);
URI = uri_temp.AbsoluteUri;

 string[] ext = URI.Split('.');
 //textBox1.Text = ext[0];

 string[] ss = ext[0].Split('/');
 int a = ss.Length;

string a1 = ss[a - 1];

string image_prefix = a1.Substring(0, 4);
   string image_no = a1.Substring(4, 3);
   for (int i = 0; i < a-1; i++)
    {
        path = path + ss[i] + "/";
     }
  string path1 = path;

     path = path1 + image_prefix + image_no + "." + ext[1];

   for (int i = 1; i < 999; i++)
    {
        if (i < 10)
       {
           image_no = "00" + i;
        }
       else if (i < 100)
      {
           image_no = "0" + i;
         }
         else
        {
              image_no = i.ToString();
          }
          path = path1 + image_prefix + image_no + "." + ext[1];
           string dasdasd = path;

         string loc = new Uri(path).LocalPath;
           bool asasa = File.Exists(loc);
       if (asasa == true)
          {      System.Threading.Thread.Sleep(5000);
                image1.Source = new BitmapImage(new Uri(dasdasd));
           }
             else
             {
                System.Windows.Forms.MessageBox.Show("File not found");
         }

But the image is not getting displayed. do the need ful....!!

Upvotes: 0

Views: 591

Answers (2)

Clemens
Clemens

Reputation: 128087

Use a DispatcherTimer to update the currently displayed image.

private DispatcherTimer timer = new DispatcherTimer();
private int imageIndex;
private int maxImageIndex;

public MainWindow()
{
    InitializeComponent();
    timer.Tick += TimerTick;
}

private void StartSlideShow(TimeSpan interval, int maxIndex)
{
    imageIndex = 0;
    maxImageIndex = maxIndex;

    timer.Interval = interval;
    timer.Start();
}

private void TimerTick(object sender, EventArgs e)
{
    image.Source = new BitmapImage(new Uri(CreatePath(imageIndex)));

    if (++imageIndex >= maxImageIndex)
    {
        ((DispatcherTimer)sender).Stop();
    }
}

private string CreatePath(int index)
{
    // create image file path from index
    // ...
}

Start displaying images by calling for example

StartSlideShow(TimeSpan.FromSeconds(5), 1000);

Upvotes: 1

Nico Schertler
Nico Schertler

Reputation: 32627

With the call to Thread.Sleep() you block the UI thread. So whatever change you make to the UI will not be displayed.

What you should do instead is using a timer. Make i (you may want to rename it) a class variable. Set the timer to fire up every 5 seconds. In the timer event, increment i, load the next image and set it for the image control. To set the image you have to use the Dispatcher (Dispatcher.Invoke()), because you cannot change the UI from a non-UI thread.

Upvotes: 0

Related Questions