Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gif Animated Files in C# have Lower framerates than they should

ive got a full screen loading page after my software's done running,which includes a preloader gif file .... which is exactly like the one windows 8 is using for store and metro.

however the problem is that no matter what the fps and speed of the gif file is,the C# windows form will show the same lower fps.

i really dont know what to do with it as i've tried to search the internet to find a solution but whatever i ran into was either unclear or unrelated.

i suspect that somehow the compiler is ignoring the fps integrated within the gif file i imported. but i do not know how i can set the fps to what it is for the file,or make the IDE/Compiler ignore its rules when it comes to that file..please advise <3

here is the link to the file properties im using http://preloaders.net/en/search/windows%208 the one in the middle from the top row...it has 75 frames and the rest is as it is...

im currently using a picturebox control to include the gif in my project (its within a winform project)

like image 912
Niklas Avatar asked Dec 14 '22 20:12

Niklas


1 Answers

This is a normal mishap, GIFs specify the display time for a frame in units of 10 milliseconds. But timers on Windows, by default, are not sufficiently accurate to time intervals that low. Timers are updated by the system clock interrupt, it ticks 64 times per second by default. Giving timers an accuracy no better than 15.625 milliseconds. In effect, your GIF will playback at 2/3rd speed, give or take.

You can change the outcome by starting another program, like Windows Media Player or a browser like Firefox or Chrome. And you'll suddenly see your GIF playing back at design speed.

You'll have to do the same thing these programs do, alter the clock interrupt rate to get consistent playback speeds. You need to pinvoke timeBeginPeriod, pass 10 to improve the timer accuracy to 10 milliseconds. You may need to go lower if your UI thread isn't responsive and starts the timer too late. Be sure to pinvoke timeEndPeriod() again when you no longer need the playback, typically in the FormClosed event handler.

Boilerplate code:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        timeBeginPeriod(timerAccuracy);
    }

    protected override void OnFormClosed(FormClosedEventArgs e) {
        timeEndPeriod(timerAccuracy);
        base.OnFormClosed(e);
    }

    // Pinvoke:
    private const int timerAccuracy = 10;
    [System.Runtime.InteropServices.DllImport("winmm.dll")]
    private static extern int timeBeginPeriod(int msec);
    [System.Runtime.InteropServices.DllImport("winmm.dll")]
    public static extern int timeEndPeriod(int msec);
}
like image 160
Hans Passant Avatar answered May 01 '23 13:05

Hans Passant