Search code examples
c#winformsanimated-gif

C# How to stop animated gif from continually looping


I have an event that gets called when my app uses the internet. The event changes an animated gif composed of 7 frames. How do I make it only loop through the frames only once?

        private void trafficSendingActive(object sender, trafficEventArgs e)
        {
            txImage.Image = Properties.Resources.blip;
        }

Solution

  • You could use the System.Drawing.ImageAnimator to start/stop the gif animation

    // start
    System.Drawing.ImageAnimator.Animate(txImage.Image, OnFrameChanged);
    
    // stop
    System.Drawing.ImageAnimator.StopAnimate(txImage.Image, OnFrameChanged);
    
    private void OnFrameChanged(object sender, EventArgs e)
    {
       // frame change
    }