在WPF中以120FPS播放图像序列

时间:2017-04-10 11:06:48

标签: c# wpf image

我尝试创建一个程序,该程序采用图像序列并以所需的帧速率播放(最高120 FPS)。

到目前为止,我有一个类,Image控件的Source被绑定。此类存储文件位置列表,并可以调用下一帧的文件位置。它看起来像这样:

public class VideoViewer : INotifyPropertyChanged
{
    private string image;
    private List<string> imageLocs = new List<string>();
    private int imageIndex = 0;

    public event PropertyChangedEventHandler PropertyChanged;

    public string imageSource
    {
        get
        {
            return image;
        }
        set
        {
            image = value;
            OnPropertyChanged("imageSource");
        }
    }

    public List<string> imagesArray
    {
        get
        {
            return imageLocs;
        }
        set
        {
            imageLocs = value;
            OnPropertyChanged("imagesArray");
        }
    }

    public void NextFrame()
    {
        //If not on last frame
        if(imageIndex < (imageLocs.Count - 1))
        {
            //Add one to the index to select the next frame in the array
            imageIndex += 1;
            imageSource = imagesArray[imageIndex];

            //Update the image
            OnPropertyChanged("imageSource");
        }
        //If on the last frame of the array, reset to 0
        else if(imageIndex == (imageLocs.Count - 1))
        {
            imageIndex = 0;
            imageSource = imagesArray[imageIndex];
            OnPropertyChanged("imageSource");
        }
    }

    // Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

在我的MainWindow中,我有3个按钮,一个加载类的浏览按钮,一个启动DispatcherTimer的播放按钮,每隔1/120秒调用我的类的NextFrame()方法,以及一个停止计时器的停止按钮

public partial class MainWindow : Window
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    VideoViewer vV = new VideoViewer();
    DispatcherTimer timer = new DispatcherTimer();

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = vV;
    }

    private void PlayButton_Click(object sender, RoutedEventArgs e)
    {
        timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1/120) };
        timer.Tick += TimerTick;
        timer.Start();
    }

    private void PauseButton_Click(object sender, RoutedEventArgs e)
    {
        timer.Stop();
    }

    private void TimerTick(object sender, EventArgs e)
    {
        vV.NextFrame();
    }

    private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {
        // Set the file dialog to filter for graphics files.
        this.openFileDialog1.Filter =
            "Images (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|" +
            "All files (*.*)|*.*";

        // Allow the user to select multiple images.
        this.openFileDialog1.Multiselect = true;
        this.openFileDialog1.Title = "My Image Browser";

        DialogResult dr = this.openFileDialog1.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                List<string> images = new List<string>();

                // Read the files
                foreach (String file in openFileDialog1.FileNames)
                {
                    images.Add(file);
                }

                vV.imagesArray = images;
            }
        }
    }
}

这可以在较低的帧速率下正常工作,但似乎不会以超过30 FPS的速度播放视频(例如,当设置为120 FPS时,我预计30 FPS视频将以4倍速播放)

我可以使用除DispatcherTimer以外的其他东西来达到预期的效果吗?

1 个答案:

答案 0 :(得分:0)

不幸的是,DispatcherTimer的Interval属性不够精确。

  1. 它永远不会在指定的时间间隔启动之前触发“ Tick”事件,但是...
  2. 很有可能会比间隔时间长15-25ms,因为还有其他东西要完成。

如果您使用它,它可能会稍微改善...

DispatcherTimer dt = new DispatcherTimer(DispatcherPriority.Send);

...增加优先级,但没有达到120FPS(8.3ms间隔)。

也许这会有所帮助: 无需订阅DispatcherTimer的“ Tick”事件,您可以订阅...

System.Windows.Media.CompositionTarget.Rendering += DoSomething;

...至少在现代PC上以相当可靠的60FPS触发事件。