0

I am new to C# and i am willing to learn it by creating different projects (I am working at one right now). I am trying to make a picture box show different frames of a gif file. I have tried doing this with FrameDimension and SelectActiveFrame but it won't work. I have also tried to split the frames of the gif and separate them into different .png files. I have added them in my Resources folder in my project and i have tried to make the picture box show them. Here is the code I wrote:

public void Form1
{
InitializeComponent();
DrawPNG();
}

public void DrawPNG()
{
        Bitmap bmp = Properties.Resources.pic1;
        picBox1.Image = bmp;

        bmp = Properties.Resources.pic2;
        picBox1.Image = bmp;

        bmp = Properties.Resources.pic3;
        picBox1.Image = bmp;

        bmp = Properties.Resources.pic4;
        picBox1.Image = bmp;
    }

The problem is that the picture box will constantly show the first frame and it won't change. I am out of ideas of how to do this and I have looked up on the internet and haven't found anything. Can someone help me please?

Oh, and by the way, I am working on a Windows Forms Application.

  • You need to do all painting in the `Paint` event. In other places, invalidate the picturebox to trigger a paint message. – xxbbcc Nov 10 '17 at 18:56
  • And here you're just overwriting the `Image` property and never give it time to display. `PictureBox` is not an animation player - you have to code it to play multiple frames over time. – xxbbcc Nov 10 '17 at 18:58
  • So, I should try and use Thread.Sleep(); , for example? – TatsuyaShiba123 Nov 10 '17 at 19:01
  • 2
    Absolutely not. You should almost _never_ use `Thread.Sleep`. Use a UI timer. – xxbbcc Nov 10 '17 at 19:01
  • And if, for example, when I move the picture box towards another one, do the picture boxes override? – TatsuyaShiba123 Nov 10 '17 at 19:16
  • I don't know what you mean but if you want to do animation, don't use multiple pictureboxes - use a single one and render the relevant part of all bitmaps into it as you update the animation. – xxbbcc Nov 10 '17 at 19:17
  • To animate, you can loop your images or explicitly set them inline as here, except use an `async` method, and call `await Task.Delay(...)` in between each frame. That said, `PictureBox` normally will animate GIF files automatically all on its own. You shouldn't have to implement anything to get this to work. Just load an animated GIF as your `Bitmap` object and assign it to the `PictureBox`. If you can't get that to work, you need to post a question with a good [mcve] that reliably reproduces that problem. – Peter Duniho Nov 11 '17 at 01:16

0 Answers0