WPF显示每个图像x秒如何?

时间:2013-04-23 11:49:52

标签: c# wpf windows image

我正在尝试使用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
    }
}

4 个答案:

答案 0 :(得分:1)

将图片路径保存在列表和列表中使用计时器的tick事件。 类似的东西:

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];   
}

答案 1 :(得分: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
}

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这样的事情:

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