Advertisement
Guest User

Untitled

a guest
Jul 21st, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. public static void Main()
  2. {
  3.     var frameList = Gif.LoadAnimatedGif ("b.gif");
  4.  
  5.     var i = 0;
  6.     foreach(var frame in frameList)
  7.         frame.Image.Save ("frame_" + i++ + ".png");
  8. }
  9. public class Gif
  10. {
  11.     public static List<Frame> LoadAnimatedGif(string path)
  12.     {
  13.         //If path is not found, we should throw an IO exception
  14.         if (!File.Exists(path))
  15.             throw new IOException("File does not exist");
  16.  
  17.         //Load the image
  18.         var img = Image.FromFile(path);
  19.  
  20.         //Count the frames
  21.         var frameCount = img.GetFrameCount(FrameDimension.Time);
  22.  
  23.         //If the image is not an animated gif, we should throw an
  24.         //argument exception
  25.         if (frameCount <= 1)
  26.             throw new ArgumentException("Image is not animated");
  27.  
  28.         //List that will hold all the frames
  29.         var frames = new List<Frame>();
  30.  
  31.         //Get the times stored in the gif
  32.         //PropertyTagFrameDelay ((PROPID) 0x5100) comes from gdiplusimaging.h
  33.         //More info on http://msdn.microsoft.com/en-us/library/windows/desktop/ms534416(v=vs.85).aspx
  34.         var times = img.GetPropertyItem(0x5100).Value;
  35.  
  36.         //Convert the 4bit duration chunk into an int
  37.  
  38.         for (int i = 0; i < frameCount; i++)
  39.         {
  40.             //convert 4 bit value to integer
  41.             var duration = BitConverter.ToInt32(times, 4*i);
  42.  
  43.             //Add a new frame to our list of frames
  44.             frames.Add(
  45.                 new Frame()
  46.                 {
  47.                 Image = new Bitmap(img),
  48.                 Duration = duration
  49.             });
  50.  
  51.             //Set the write frame before we save it
  52.             img.SelectActiveFrame(FrameDimension.Time, i);
  53.  
  54.  
  55.         }
  56.  
  57.         //Dispose the image when we're done
  58.         img.Dispose();
  59.  
  60.         return frames;
  61.     }
  62. }
  63.  
  64. //Class to store each frame
  65. public class Frame
  66. {
  67.     public Bitmap Image { get; set; }
  68.     public int Duration { get; set;}
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement