【问题标题】:WPF Show each image x seconds how?WPF 如何显示每个图像 x 秒?
【发布时间】:2013-04-23 20:19:55
【问题描述】:

我正在尝试使用 mediaElement 制作幻灯片,在列表框 x 秒内显示每个图像。

如何让我的代码在继续播放前 x 秒播放每个图像?

此代码将所有图像添加到名为 Listbox1 的列表框

    Dictionary<string, string> Listbox1Dict = new Dictionary<string, string>();

    private void SearchBtn_Click(object sender, RoutedEventArgs e)
    {
        Listbox1.Items.Clear();
        FolderBrowserDialog folderDialog = new FolderBrowserDialog();
        folderDialog.SelectedPath = "C:\\";

        DialogResult result = folderDialog.ShowDialog();
        if (result.ToString() == "OK")
            FileNameTextBox.Text = folderDialog.SelectedPath;
        string directory = FileNameTextBox.Text;
        var files = Directory.GetFiles(directory).Where(name => !name.EndsWith(".ini"));
        foreach (string file in files)
        {
            Listbox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
            Listbox1Dict.Add(System.IO.Path.GetFileNameWithoutExtension(file), file);
        }
    }

此代码全屏显示所有图像,但它会在开始时跳过每个人到最后一张图像。

private void button1_Click_1(object sender, RoutedEventArgs e)
{
    foreach (var selected in Listbox1.Items)
        {
            string s = selected.ToString();
            if (Listbox1Dict.ContainsKey(s))
            {
                mediaElement1.Visibility = Visibility.Visible;
                SearchBtn.Visibility = Visibility.Hidden;
                Listbox1.Visibility = Visibility.Hidden;
                FileNameTextBox.Visibility = Visibility.Hidden;
                mediaElement1.Source = new Uri(Listbox1Dict[s]);
                mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
                this.Background = new SolidColorBrush(Colors.Black);
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
            }

    }
}

尝试使用此代码使图像一张一张播放,但出现错误。看代码评论:

private int currentSongIndex = -1;

void mediaElement1next(object sender, EventArgs e)
{
    if(currentSongIndex == -1)
    {
        currentSongIndex = Listbox1.SelectedIndex;
    }
    currentSongIndex++;
    if(currentSongIndex < Listbox1.Items.Count)
    {
        mediaElement1.Play(Listbox1.Items[currentSongIndex]); // No overload for method 'Play' takes 1 arguments    
    }
    else
    {
        // last song in listbox has been played
    }
}

【问题讨论】:

  • 您需要在某处引入暂停。也许使用 AutoResetEvent 会有所帮助,也许会有所帮助。

标签: c# wpf windows image


【解决方案1】:

我认为您需要一个计时器来设置下一张图片。使用您当前使用的代码,它将遍历您的列表并更改图像,直到您到达最后。

看看DispatcherTimer。您可以将其设置为,在每次滴答时,它都会更改为下一个图像。像这样的东西(只是写下我的头)

dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

然后,在您的事件处理程序中:

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    // get the next image
}

当然您可以使用其他类型的计时器,但这是主要思想。

【讨论】:

  • 而不是foreach,有一个索引n,并在每次计时器滴答时增加它。然后,要获取您的图像,您需要获取列表框的 nth 元素,而不是遍历它。
  • 更新了我的代码,再次查看我的帖子。那么我该如何处理这个问题呢? ://
  • 这出戏从何而来?我以为您只需要更改图像的Source,就像您在上面所做的那样。
【解决方案2】:

将您的图像路径保存在列表中并使用计时器的滴答事件。 比如:

List<string> paths = new List<string>();

private void timer1_Tick(object sender, EventArgs e)
{
    pictureBox1.Image = getNextImage();
}

private string getNextImage()
{
    //code...
}
enter code here

编辑: 添加一个类变量:int index = 0; 在 SearchBtn_Click 事件中,将结果添加到列表中。

//..
    foreach (string file in files)
    {
        paths.Add(file);
    }
//..

然后照我上面做的,getNextImage方法的内容就是:

private string getNextImage()
{
    if(index < paths.Count - 1)
    {
        index += 1;
    }
    else
    {
        index = 0;
    }
    return paths[index];   
}

【讨论】:

  • hm 无法使代码工作...你能用我提供的代码写一个例子吗?
  • 抱歉回复晚了,但我使用的是WPF,那里没有图片框。我正在使用媒体元素。有什么想法吗?
【解决方案3】:

我的想法是实现一个计数到 X 的线程,然后在完成后调用 NextImage() 函数。

【讨论】:

  • 是的,这也是我一直在想的,但也必须有一个“下一个图像控制”。
【解决方案4】:

类似这样的:

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (Listbox1.Items.Count > 0)
            {
                if (dispatcherTimer.IsEnabled)
                    dispatcherTimer.Stop();
                else
                {
                    curImage = 0;
                    dispatcherTimer.Start();
                }
            }
        }




private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    Dispatcher.Invoke((Action)delegate
    {
        ShowNextImage();
    }, null);            
}



private void ShowNextImage()
{
    if (curImage >= Listbox1.Items.Count)
        curImage = 0;

    var selected = Listbox1.Items[curImage];
    string s = selected.ToString();
    if (Listbox1Dict.ContainsKey(s))
    {
        mediaElement1.Visibility = Visibility.Visible;
        SearchBtn.Visibility = Visibility.Hidden;
        Listbox1.Visibility = Visibility.Hidden;
        FileNameTextBox.Visibility = Visibility.Hidden;
        mediaElement1.Source = new Uri(Listbox1Dict[s]);
        mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
        mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
        this.Background = new SolidColorBrush(Colors.Black);
        this.WindowStyle = WindowStyle.None;
        this.WindowState = WindowState.Maximized;
    }
}

和声明

DispatcherTimer dispatcherTimer = new DispatcherTimer();
        int x = 2; //seconds
        private int curImage = 0;

还有一些结构

dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, x);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-04
    • 2013-11-22
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多