简体   繁体   中英

How to stop PictureBox GIF animation after the first loop C#?

I put a GIF animation inside a PictureBox and by default my animation is continuously looping. I need to stop it after the first loop by doing this pictureBox1.Enabled=false or manually change the GIF animation to last frame of this animation, but how can i catch the moment of first loop ending?

I found this question ( click ), but i didnt understand how can i use it, could anyone make it clear or describe another way of doing this?

The solution would be to get the number of frames and then add an event to the picureBox.Paint in order to check how many frames have been rendered.Once that you have reached the number of frames you can then disable the pictureBox.

Here I have made a simple solution:

   int numberOfFrames = 0;
   int currentFrame = 0;
    public Form1()
    {
        InitializeComponent();
        pictureBox1.Paint += new PaintEventHandler(this.pictureBox1_Paint);

        FrameDimension dimension = new FrameDimension(this.pictureBox1.Image.FrameDimensionsList[0]);
        numberOfFrames = this.pictureBox1.Image.GetFrameCount(dimension);

    }

    private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        if(currentFrame == numberOfFrames)
        {
            this.pictureBox1.Enabled = false;
        }
        currentFrame++;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM