【问题标题】:Displaying all the images in a folder in image control with 5sec time gap在图像控件中以 5 秒的时间间隔显示文件夹中的所有图像
【发布时间】:2013-06-03 06:58:21
【问题描述】:

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

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");
         }

但是图像没有显示出来。满足需求……!!

【问题讨论】:

    标签: wpf c#-4.0


    【解决方案1】:

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

    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);
    

    【讨论】:

    • 谢谢克莱门斯。我用了类似的方法,不完全是这个。
    【解决方案2】:

    调用Thread.Sleep() 会阻塞UI 线程。因此,您对 UI 所做的任何更改都不会显示。

    您应该做的是使用计时器。将i(您可能想要重命名它)设为类变量。将计时器设置为每 5 秒启动一次。在计时器事件中,递增i,加载下一个图像并将其设置为图像控件。要设置图像,您必须使用 Dispatcher (Dispatcher.Invoke()),因为您无法从非 UI 线程更改 UI。

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 2014-01-18
      相关资源
      最近更新 更多