2

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

4 回答 4

1

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

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];   
}
于 2013-04-23T11:54:08.987 回答
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
}

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

于 2013-04-23T11:54:57.060 回答
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);
于 2013-04-23T12:10:07.277 回答
0

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

于 2013-04-23T11:51:45.403 回答