Skip to content

ImageAnimator's animation timing drifts #52144

Closed

Description

@jeffhandley

Description

The timing of System.Drawing.ImageAnimator.Animate drifts.

Configuration

  • .NET 6.0 (main branch), but it seems to be a longstanding issue since this code has not been changed in a long time.
  • Windows 10 and Ubuntu 20.04
  • x64
  • The behavior is currently different between Windows and Linux, but both present drifts

Regression?

No

Other information

Using the animated gifs added to runtime-assets in dotnet/runtime-assets#137, and the repro app below, I captured snapshots of animated gifs as each frame is animated. The animated gifs show timers, and the captured rendering shows the animation drifting.

Repro App

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;

namespace animatorapp
{
    class Program
    {
        static DateTime StartTime;
        static string OutputFolder;
        static Bitmap GIF;
        static List<(string Timestamp, Image Thumbnail)> Snapshots = new();

        static void Main(string[] args)
        {
            if (args.Length < 2 || !File.Exists(args[0]))
            {
                throw new ArgumentException("Syntax: appanimator imageFile outputFolder [seconds]. " + string.Join(",", args));
            }

            int seconds = args.Length >= 3 ? int.Parse(args[2]) : 10;
            GIF = new(args[0]);

            StartTime = DateTime.Now;
            SaveSnapshot(GIF);

            ImageAnimator.Animate(GIF, new EventHandler(OnAnimated));

            Thread.Sleep(seconds * 1000);

            ImageAnimator.StopAnimate(GIF, OnAnimated);

            OutputFolder = Path.Combine(args[1], StartTime.ToString("yyyy-MM-dd-HH-mm"));
            Directory.CreateDirectory(OutputFolder);

            foreach (var (timestamp, snapshot) in Snapshots)
            {
                snapshot.Save(Path.Combine(OutputFolder, $"{timestamp}.png"), ImageFormat.Png);
            }
        }

        private static void SaveSnapshot(Image image)
        {
            Snapshots.Add(((DateTime.Now - StartTime).ToString(@"ss\.ff"), image.GetThumbnailImage(128, 128, null, IntPtr.Zero)));
        }

        private static void OnAnimated(object o, EventArgs e)
        {
            Bitmap image = (Bitmap)o;
            ImageAnimator.UpdateFrames(image);
            SaveSnapshot(image);
        }
    }
}

Expected Result

The expected result of this repro app is that the captured thumbnails are generally aligned to the natural timing of the animation, without presenting a drift over time.

Actual Result

The shorter the delays between the frames in the animation, the more pronounced the drift. For example, a ~100fps animation shows much more drift than a ~1fps animation.

100fps Animation

Clock Time 0.00 seconds 1.82 seconds 1.88 seconds 4.94 seconds
Animation Time 0.00 seconds 0.98 seconds 1.27 seconds 2.66 seconds
Snapshot

1fps Animation

Clock Time 0.00 seconds 1.20 seconds 1.25 seconds 9.35 seconds
Animation Time 0.00 seconds < 1 second ~ 1 second ~ 7 seconds
Snapshot

Activity

  1. ghost added
    untriaged
    on Apr 30, 2021
  2. self-assigned this
    on Apr 30, 2021
  3. jeffhandley commented on May 1, 2021

    @jeffhandley
    MemberAuthor

    Here's what I found from the current implementation:

    1. The animation is relying on Thread.Sleep(50) to be very accurate; it's not
    2. Once a frame's time has elapsed, the animation very naively progresses 1 frame and sets the next frame's timer to 0
      • This does not account for the scenario of the thread having slept longer than a frame's duration
      • It also causes any leftover elapsed time to be lost
    3. When animation begins, it immediately progresses instead of initially sleeping
      • This results in the initial frame being skipped if its duration is less than 50ms

    These factors added up to make animation timing quite incorrect.

  4. ghost added
    in-pr
    on May 4, 2021
  5. removed
    untriaged
    on May 5, 2021
  6. added this to the Future milestone on May 5, 2021
  7. ghost removed
    in-pr
    on May 6, 2021
  8. ghost locked as resolved and limited conversation to collaborators on Jun 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions