none
ImageAnimator: get current playing frame of a gif file RRS feed

  • Question

  • Greetings,

    I'm using ImageAnimator to play a gif file in a picture box. How can I check if ,for example, frame number 20 is now being played ? is there a method like Image.GetFrameCount ?

    Wednesday, August 7, 2019 3:07 AM

All replies

  • Since it doesn't have such property exposed, and the OnFrameChanged event handler is only ordinary EventHandler and does not return current frame index, I think you have to count it yourself on the OnFrameChanged handler passed in Animate method.
    Wednesday, August 7, 2019 3:15 AM
    Answerer
  • Since it doesn't have such property exposed, and the OnFrameChanged event handler is only ordinary EventHandler and does not return current frame index, I think you have to count it yourself on the OnFrameChanged handler passed in Animate method.

    Good idea. But doesn't it make animating process run slower ?



    • Edited by Kevin993 Wednesday, August 7, 2019 11:10 AM
    Wednesday, August 7, 2019 11:09 AM
  • Good idea. But doesn't it make animating process run slower ?


    You just add a counter and re-initialize it when it is >= Image.GetFrameCount


    Wednesday, August 7, 2019 1:49 PM
  • Good idea. But doesn't it make animating process run slower ?


    You just add a counter and re-initialize it when it is >= Image.GetFrameCount


    What I actually did is :

    Public index1 as integer = 0
    
    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)
         
    Picturebox1.Invalidate()
    index1 +=1
    End Sub

    by tracking index1 value at the moment, it is possible to see what frame index is currently being played,

    But logically, by adding "index1 +=1" , we are actually adding an additive process , though really simple,  while animating frames.


    Wednesday, August 7, 2019 10:19 PM
  • Hi Kevin993, 

    Thank you for posting here.

    For your question, you want to check what number of frame is being played in your gif file.

    I make a test on my side, and when you click the button you can see a specific frame of a GIF image.

    Here’s my code:

            Image img = Image.FromFile(@"D:\Test\GIF.gif");
    
            public Form1()
            {
                InitializeComponent();
                pictureBox1.Image = img;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Image[] frames = new Image[36];
                FrameDimension dimension = new FrameDimension(img.FrameDimensionsList[0]);
                int frameCount = img.GetFrameCount(dimension);
                label1.Text = frameCount.ToString();
    
                img.SelectActiveFrame(dimension, 1);
                frames[1] = ((Image)img.Clone());
                pictureBox1.Image = frames[1];
                pictureBox1.Enabled = false;
            }

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 8:19 AM
    Moderator