Last active
October 7, 2015 01:38
-
-
Save JamesSkemp/64a27971b85a72f5b088 to your computer and use it in GitHub Desktop.
Extract frames from GIF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var imagePath = @"C:\Users\James\Desktop\HL0hEkH.gif"; | |
var exportDirectory = @"C:\Users\James\SkyDrive\Projects\graphic samples\export\"; | |
var exportPathFormat = exportDirectory + "{0}.png"; | |
Image img = Image.FromFile(imagePath); | |
var totalFrames = img.GetFrameCount(FrameDimension.Time); | |
string.Format("Total frames: {0}", totalFrames).Dump(); | |
// Zero-based. | |
int frameToGet = 0; | |
if (frameToGet >= img.GetFrameCount(FrameDimension.Time) || frameToGet < 0) { | |
frameToGet = 0; | |
} | |
var frameDimension = new FrameDimension(img.FrameDimensionsList[0]); | |
img.SelectActiveFrame(frameDimension, frameToGet); | |
(new Bitmap(img)).Dump(); | |
if (!Directory.Exists(exportDirectory)) { | |
Directory.CreateDirectory(exportDirectory); | |
} | |
for (int i = 0; i < totalFrames; i++) | |
{ | |
img.SelectActiveFrame(frameDimension, i); | |
(new Bitmap(img)).Save(string.Format(exportPathFormat, i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment