PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 18, 2022

[FIXED] How to split a .gif in frames?

 July 18, 2022     c#, frame, gif, image     No comments   

Issue

All the codes I've found give me the same result: a bunch of exact looking frames. Meaning: it gives me a list of the first frame, repeated X number of times. The .gif I'm using has 30 frames, so I get 30 times the first frame, instead of the 30 different frames.

    public static Image[] GetFramesFromAnimatedGIF(Image IMG)
    {
        List<Image> IMGs = new List<Image>();
        int Length = IMG.GetFrameCount(FrameDimension.Time);

        for (int i = 0; i < Length; i++)
        {
            IMG.SelectActiveFrame(FrameDimension.Time, i);
            IMGs.Add(IMG);
        }

        return IMGs.ToArray();
    }

What am I missing? ALL the codes I've looked give the first frame repeated X numbers of times.

This is what is supposed to look (using a webpage). See how each frame is different?

enter image description here

This is what it looks for me after saving every frame inside that array on a folder location (a bunch of equal frames):

enter image description here

P.S.: Yes, it's a .gif the image I'm using.

Update: The problem seems to be when I read the file in the OpenFileDialog, as it works if I pass my .gif by code. So how do I read an animated gif in the OpenFileDialong? Thank you.


Solution

   IMGs.Add(IMG);

That's the bug, you are adding the same IMG object over and over again. You need to make a deep copy of the frame. That's very easy to do:

   IMGs.Add(new Bitmap(IMG));


Answered By - Hans Passant
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing