Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a .gif in frames?

Tags:

c#

image

gif

frame

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.

like image 430
soulblazer Avatar asked Oct 11 '15 06:10

soulblazer


People also ask

Can I cut in GIF?

GIF cutter (cut to length)You can remove the beginning or end of the GIF, or cut out the middle part. You can either specify the cut duration in seconds or enter the exact frame numbers where you want the GIF to be cut. If you want to cut file dimensions instead of length, you should use our crop tool instead.


1 Answers

   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));
like image 166
Hans Passant Avatar answered Sep 22 '22 05:09

Hans Passant