0

I'm trying to create add a super simple animated gif to a PictureBox control in WinForms.

And I'm having this super weird issue, that the gif runs twice, even though the gif is created to only run once.

This is the code. I tried adding it through Resources and the Design window, same result.

    private string gif = AppDomain.CurrentDomain.BaseDirectory + @"resources\lock.gif";

    public App()
    {
        InitializeComponent();
        // picturebox is set to disabled in properties
        pbxImage.ImageLocation = gif;
        pbxImage.Load();
        pbxImage.Enabled = true;
        
    }

I was thinking this would be super simple, but I can't for the life of me figure out why. I'm relatively new to WinForms so I'm guessing there's something I'm missing.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Daniel D.
  • 19
  • 5
  • @Austin Yeah - checked, and rechecked with Photoshop - even edited the gif and re-exported it. Same result. – Daniel D. Jan 10 '22 at 22:19
  • Try with another gif - see if you get the same result. That experiment should reveal if it's your WinForms or your gif that is the problem – Jens Kloster Jan 10 '22 at 22:25
  • I tried both answers, without any success. In the latter - I tried using the first frame as my initial frame, but to no avail. It just insists on playing the whole .gif twice. I really over an hour of research trying to figure out why. I was hoping there was some setting I forgot or didn't know about. Thanks anyhow! – Daniel D. Jan 10 '22 at 22:29
  • 1
    @JensKloster - I tried with several, same result. – Daniel D. Jan 10 '22 at 22:29
  • Shot in the dark, try using the `OnShown(...)` override instead. – LarsTech Jan 10 '22 at 22:32
  • @LarsTech Giving that a shot. – Daniel D. Jan 10 '22 at 22:33
  • @LarsTech No dice, unfortunately. – Daniel D. Jan 10 '22 at 22:36
  • [How to play a GIF animation to the last Frame, then stop the animation?](https://stackoverflow.com/a/67305196/7444103) (C# code in the second snippet) - You can also control how many times the animation loops. – Jimi Jan 10 '22 at 22:38
  • @Jimi - I can make something of this, this actually stops my gif, though in a super weird place. Stops it at frame 4 somehow. But I can work with that. – Daniel D. Jan 10 '22 at 22:49
  • @Austin - I'll give that a shot. – Daniel D. Jan 10 '22 at 22:49
  • @Austin - No dice. This shouldn't be that hard, should it? I even tried with a gif of my own making, same result. – Daniel D. Jan 10 '22 at 22:53
  • This might be a sign that the GIF is not 100% compatible. Try with another (e.g., download one from Google Images) to see what happens. – Jimi Jan 10 '22 at 22:55
  • @Jimi - I tried several already, even made one quickly, same result. – Daniel D. Jan 10 '22 at 22:59
  • Well, I tested a few myself when I wrote that code and all worked as intended. Did you follow the *instructions* there? For example, start the animation using a Button as described (for testing), or in the `Form.Load` or `Form.Shown` handlers. – Jimi Jan 10 '22 at 23:02
  • @Jimi - Yeah, I did. Project is targeting .net 6 though. I'm creating a project targeting .net 5, seeing if that fixes it. Trust me, I'm super confused. I know this question looks super simple, and it should be. I spent a lot of time reseaching before actually asking a question here. Tried OnLoad, OnShown, OnActivated (On the PictureBox) - even tried from a different thread. – Daniel D. Jan 10 '22 at 23:07

0 Answers0