How to extract frames from gif

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Bitmap b=new Bitmap((Image)Ims[0]);
IEnumerator E= (b).FrameDimensionsList.GetEnumerator();
E.MoveNext();
b= (Bitmap)E.Current;

Invalid cast exception. I must be doing this wrong. I have look all over the net form information on how to do this but could find nothing.
 

aewarnick

Senior Contributor
Joined
Jan 29, 2003
I just opended an animated gif in IrfanView and extracted all the frames.

How do I do that in my program is what I am asking?
 

-Strict-

Freshman
Joined
Jan 18, 2003
try this

Code:
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Windows.Forms

Public Module test
	Sub Main()
		Application.Run(New myform)
	End Sub
End Module

Public Class myform
	Inherits System.Windows.Forms.Form
	
        Private components As System.ComponentModel.Container
	Private newimg As Bitmap = New Bitmap("C:\l5r\anielg.gif")
	
	Public Sub New()
	
	
	        MyBase.New()
	        Me.components = New System.ComponentModel.Container()
		ImageAnimator.Animate(newimg, New EventHandler(AddressOf Me.go))
	End Sub
	
	Protected Overrides Sub OnPaint(e As PaintEventArgs)
	    	ImageAnimator.UpdateFrames()
	    	e.Graphics.DrawImage(newimg, 20, 20)
	End Sub
	
	Public Sub go(ByVal sender As Object, ByVal e As EventArgs)
		' Without this line, the form will not keep fireing OnPaint
		Me.Invalidate()
	End Sub
End Class
 

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Thank you but I wasn't looking to animate the frames but to extract them from an already existing gif, saving them as individual files.
 

-Strict-

Freshman
Joined
Jan 18, 2003
I think you should be able to 'adapt' it to do that.

If you look in the paint sub, it extracts each frame and paints it to the control. I guess all you would need to do is whatever you wanted to do with the image object before.. save it, convert it or whatever..


hope it helps.. I can't explain the code in detail as I found it myself while looking up code for manipulating GIFs.
 

SilverX

Newcomer
Joined
Jan 23, 2010
Here is the code I use to extract GIF frames

EDIT: just so you are aware Image.FrameDimensionsList is an array of Guid's not Images/Bitmaps

Code:
class Frame
{
     public int Duration;
     public Bitmap Image;

     public Frame(int duration, Bitmap image)
     {
          Image = image;
          Duration = duration;
     }
}

// implementation
Frame[] frames;

FrameDimension fd = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(fd);

if (frameCount > 1)
{
      frames = new Frame[frameCount];

      //0x5100 is the property id of the GIF frame's durations
      //this property does not exist when frameCount <= 1
      byte[] times = image.GetPropertyItem(0x5100).Value;

      for (int i = 0; i < frameCount; i++)
      {
            //selects GIF frame based on FrameDimension and frameIndex
            image.SelectActiveFrame(fd, i);

            //length in milliseconds of display duration
            int length = BitConverter.ToInt32(times, 4 * i) * 10;

            //save currect image frame as new bitmap
            frames[i] = new Frame(length, new Bitmap(image));
       }
} // Not animated
 
Top Bottom