在图像控制中以5秒时间间隔显示文件夹中的所有图像 [英] Displaying all the images in a folder in image control with 5sec time gap

查看:162
本文介绍了在图像控制中以5秒时间间隔显示文件夹中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含1000张图像的文件夹,其图像名称为ICON000,ICON001至ICON 999。我需要他们显示在我的WPF图像控制顺序与5秒的时间延迟。我已经使用文件对话框来获取特定文件夹的路径和图像(ICON)的相应前缀。我使用下面的代码

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....!!

推荐答案

使用 DispatcherTimer 更新当前显示的图片。

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
    // ...
}

通过调用开始显示图像

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

这篇关于在图像控制中以5秒时间间隔显示文件夹中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆