Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DeGiffer!
- * This programs splits a .gif file into its separate frames.
- * Created by Ichabod Clay 10-27-2014
- *
- * Feel free to edit or play around with this to your heart's content.
- */
- using System;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- namespace DeGiffer
- {
- class MainClass
- {
- static void Main(string[] args)
- {
- if (args.Length < 1)
- {
- Console.WriteLine("Drag and drop the .gif file to be de-giffed onto this executable.");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- return;
- }
- FileInfo curFile = new FileInfo(args[0]);
- if (!curFile.Exists)
- {
- Console.WriteLine("Input file does not exist.");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- return;
- }
- if (curFile.Extension != ".gif")
- {
- Console.WriteLine("This is the de-giffer!");
- Console.WriteLine("You gotta use a gif in the de-giffer!");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- return;
- }
- if (Helpers.IsDirWriteProtected(curFile.DirectoryName))
- {
- Console.WriteLine("The directory that the input file in is write protected.");
- Console.WriteLine("Please place the input file in a writeable directory and try again.");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- return;
- }
- //If we got here, then we're good to go!
- DirectoryInfo workingDir = Directory.CreateDirectory(String.Format("{0}\\{1}_degif", curFile.DirectoryName, Path.GetFileNameWithoutExtension(curFile.FullName)));
- using (Bitmap bmp = new Bitmap(curFile.FullName))
- {
- int frameCount = bmp.GetFrameCount(FrameDimension.Time);
- int numDigits = Helpers.GetNumberOfDigits(frameCount); //this is just to help formatting the display.
- for (int i = 0; i < frameCount; i++)
- {
- Helpers.WriteProcessingInfo(i, frameCount, numDigits);
- bmp.SelectActiveFrame(FrameDimension.Time, i);
- bmp.Save(String.Format("{0}\\{1}_{2}.jpg", workingDir.FullName, Path.GetFileNameWithoutExtension(curFile.FullName), (i + 1).ToString("D" + numDigits)), ImageFormat.Jpeg);
- }
- }
- Console.WriteLine("\r\nImage processed!");
- Console.WriteLine("Press any key to exit...");
- Console.ReadKey();
- return;
- }
- }
- static class Helpers
- {
- public static bool IsDirWriteProtected(string directory)
- {
- string tempFile = String.Format("{0}\\temp", directory);
- try
- {
- File.WriteAllBytes(tempFile, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
- }
- catch (IOException)
- {
- if (File.Exists(tempFile))
- File.Delete(tempFile);
- return true;
- }
- if (File.Exists(tempFile))
- File.Delete(tempFile);
- return false;
- }
- public static int GetNumberOfDigits(int input)
- {
- return input.ToString().Length;
- }
- public static void WriteProcessingInfo(int curFrame, int totalFrames, int numDigits)
- {
- if (curFrame == 0)
- {
- Console.Write("Processing frame {0} out of {1}.", (curFrame + 1).ToString("D" + numDigits.ToString()), totalFrames);
- }
- else
- {
- Console.CursorLeft = 17;
- Console.Write(new String(' ', numDigits));
- Console.CursorLeft = 17;
- Console.Write((curFrame + 1).ToString("D" + numDigits.ToString()));
- Console.CursorLeft = 26 + numDigits * 2;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement