Advertisement
Ichabod2032

DeGiffer - Split a GIF into frames

Oct 27th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. /*
  2. *  DeGiffer!
  3. *  This programs splits a .gif file into its separate frames.
  4. *  Created by Ichabod Clay  10-27-2014
  5. *  
  6. *  Feel free to edit or play around with this to your heart's content.
  7. */
  8.  
  9. using System;
  10. using System.IO;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13.  
  14. namespace DeGiffer
  15. {
  16.     class MainClass
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             if (args.Length < 1)
  21.             {
  22.                 Console.WriteLine("Drag and drop the .gif file to be de-giffed onto this executable.");
  23.                 Console.WriteLine("Press any key to exit...");
  24.                 Console.ReadKey();
  25.                 return;
  26.             }
  27.  
  28.             FileInfo curFile = new FileInfo(args[0]);
  29.  
  30.             if (!curFile.Exists)
  31.             {
  32.                 Console.WriteLine("Input file does not exist.");
  33.                 Console.WriteLine("Press any key to exit...");
  34.                 Console.ReadKey();
  35.                 return;
  36.             }
  37.  
  38.             if (curFile.Extension != ".gif")
  39.             {
  40.                 Console.WriteLine("This is the de-giffer!");
  41.                 Console.WriteLine("You gotta use a gif in the de-giffer!");
  42.                 Console.WriteLine("Press any key to exit...");
  43.                 Console.ReadKey();
  44.                 return;
  45.             }
  46.  
  47.             if (Helpers.IsDirWriteProtected(curFile.DirectoryName))
  48.             {
  49.                 Console.WriteLine("The directory that the input file in is write protected.");
  50.                 Console.WriteLine("Please place the input file in a writeable directory and try again.");
  51.                 Console.WriteLine("Press any key to exit...");
  52.                 Console.ReadKey();
  53.                 return;
  54.             }
  55.  
  56.             //If we got here, then we're good to go!
  57.  
  58.             DirectoryInfo workingDir = Directory.CreateDirectory(String.Format("{0}\\{1}_degif", curFile.DirectoryName, Path.GetFileNameWithoutExtension(curFile.FullName)));
  59.  
  60.             using (Bitmap bmp = new Bitmap(curFile.FullName))
  61.             {
  62.                 int frameCount = bmp.GetFrameCount(FrameDimension.Time);
  63.                 int numDigits = Helpers.GetNumberOfDigits(frameCount); //this is just to help formatting the display.
  64.  
  65.                 for (int i = 0; i < frameCount; i++)
  66.                 {
  67.                     Helpers.WriteProcessingInfo(i, frameCount, numDigits);
  68.  
  69.                     bmp.SelectActiveFrame(FrameDimension.Time, i);
  70.                     bmp.Save(String.Format("{0}\\{1}_{2}.jpg", workingDir.FullName, Path.GetFileNameWithoutExtension(curFile.FullName), (i + 1).ToString("D" + numDigits)), ImageFormat.Jpeg);
  71.                 }
  72.             }
  73.  
  74.             Console.WriteLine("\r\nImage processed!");
  75.             Console.WriteLine("Press any key to exit...");
  76.             Console.ReadKey();
  77.             return;
  78.         }
  79.     }
  80.  
  81.     static class Helpers
  82.     {
  83.         public static bool IsDirWriteProtected(string directory)
  84.         {
  85.             string tempFile = String.Format("{0}\\temp", directory);
  86.  
  87.             try
  88.             {
  89.                 File.WriteAllBytes(tempFile, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF });
  90.             }
  91.             catch (IOException)
  92.             {
  93.                 if (File.Exists(tempFile))
  94.                     File.Delete(tempFile);
  95.                 return true;
  96.             }
  97.  
  98.             if (File.Exists(tempFile))
  99.                 File.Delete(tempFile);
  100.  
  101.             return false;
  102.         }
  103.  
  104.         public static int GetNumberOfDigits(int input)
  105.         {
  106.             return input.ToString().Length;
  107.         }
  108.  
  109.         public static void WriteProcessingInfo(int curFrame, int totalFrames, int numDigits)
  110.         {
  111.             if (curFrame == 0)
  112.             {
  113.                 Console.Write("Processing frame {0} out of {1}.", (curFrame + 1).ToString("D" + numDigits.ToString()), totalFrames);
  114.             }
  115.             else
  116.             {
  117.                 Console.CursorLeft = 17;
  118.                 Console.Write(new String(' ', numDigits));
  119.                 Console.CursorLeft = 17;
  120.                 Console.Write((curFrame + 1).ToString("D" + numDigits.ToString()));
  121.  
  122.                 Console.CursorLeft = 26 + numDigits * 2;
  123.             }
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement