0

Simply, I would like to get the frame rate or frames per second of a video in my WPF. In Windows or Linux, this detail can easily be found in Properties, so I'm hoping to reflect this information in my code.

The answers from here and here do not help and also, I do not work with Silverlight. The lines below cannot be recognized in C# WPF.

MediaElement1.RenderedFramesPerSecond
MediaElement1.DroppedFramesPerSecond

Would appreciate any help. Thanks!

1 Answers1

0

Most sources on the web say to use this method. I haven't tried it myself but it could give you a framerate calculation.


 CompositionTarget.Rendering += (s, a) =>
            {
                ++m_frames;
            };

            DispatcherTimer fpsTimer = new DispatcherTimer();
            fpsTimer.Interval = TimeSpan.FromSeconds(1);
            fpsTimer.Tick += (s, a) =>
            {
                lbl_fps.Content = string.Format("FPS:{0}", m_frames);
                m_frames = 0;
            };
            fpsTimer.Start();
Bron Davies
  • 5,930
  • 3
  • 30
  • 41
  • 1
    Just a nitpick (nothing serious, really): `++m_frames` is not an atomic operation. Thus, it's perfectly possible that the tick handler sets _m\_frames_ to 0 while `++m_frames` is flying, with `++m_frames` not "seeing" the reset of _m\_frames_ and setting _m\_frames_ back to the previous value+1. Not that it matters much here, as the worst that can happen is that a wrong fps value is briefly shown for a 1s only. In other application scenarios, concurrent manipulation of the same variable/field in a non-atomic unsynchronized way by multiple threads can throw a hard-to-debug spanner in the works. –  Sep 02 '22 at 14:51