Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Windows.Forms;
  11.  
  12. namespace PokemonSpriteConverter
  13. {
  14.     class Program
  15.     {
  16.         private const string DIR_SPRITES = @"\spritesToConvert";
  17.         private const string DIR_CONVERTED = @"\convertedSprites\";
  18.  
  19.         public static void Main(string[] args)
  20.         {
  21.             if (!Directory.Exists(Application.StartupPath + DIR_SPRITES))
  22.                 Directory.CreateDirectory(Application.StartupPath + DIR_SPRITES);
  23.  
  24.             if (!Directory.Exists(Application.StartupPath + DIR_CONVERTED))
  25.                 Directory.CreateDirectory(Application.StartupPath + DIR_CONVERTED);
  26.  
  27.             start();
  28.         }
  29.  
  30.         private static void start()
  31.         {
  32.             Console.WriteLine("Welcome to the Pokemon Sprite Converter. Type 'start' to begin converting from the " + DIR_SPRITES + " folder to the " + DIR_CONVERTED + " folder.");
  33.             string s = Console.ReadLine();
  34.             if (s.Equals("start"))
  35.             {
  36.                 DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + DIR_SPRITES);
  37.                 Bitmap gif = null;
  38.                 Bitmap[] frames = null;
  39.                 Bitmap converted = null;
  40.  
  41.                 foreach (FileInfo file in dirInfo.GetFiles())
  42.                 {
  43.                     gif = new Bitmap(file.FullName);
  44.                     frames = ParseFrames(gif);
  45.                     converted = new Bitmap(frames[0].Width * frames.Length, frames[0].Height);
  46.  
  47.                     for (int i = 0; i < frames.Length; i++)
  48.                         using (var g = Graphics.FromImage(converted))
  49.                             g.DrawImage(frames[i], new Rectangle(i * frames[i].Width, 0, frames[i].Width, frames[i].Height));
  50.  
  51.                     converted.Save(Application.StartupPath + @"\convertedSprites\" + file.Name.Split('.')[0] + ".png");
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 start();
  57.                 return;
  58.             }
  59.  
  60.             Console.WriteLine("Conversion complete! Press any key to continue...");
  61.             Console.ReadLine();
  62.         }
  63.  
  64.         // Credit: Answer from 'Neoheurist' in http://stackoverflow.com/questions/1712708/how-to-split-an-animated-gif-in-net
  65.         private static Bitmap[] ParseFrames(Bitmap Animation)
  66.         {
  67.             // Get the number of animation frames to copy into a Bitmap array
  68.             int Length = Animation.GetFrameCount(FrameDimension.Time);
  69.  
  70.             // Allocate a Bitmap array to hold individual frames from the animation
  71.             Bitmap[] Frames = new Bitmap[Length];
  72.  
  73.             // Copy the animation Bitmap frames into the Bitmap array
  74.             for (int Index = 0; Index < Length; Index++)
  75.             {
  76.                 // Set the current frame within the animation to be copied into the Bitmap array element
  77.                 Animation.SelectActiveFrame(FrameDimension.Time, Index);
  78.  
  79.                 // Create a new Bitmap element within the Bitmap array in which to copy the next fram
  80.                 Frames[Index] = new Bitmap(Animation.Size.Width, Animation.Size.Height);
  81.  
  82.                 // Copy the current animation frame into the new Bitmap array element
  83.                 Graphics.FromImage(Frames[Index]).DrawImage(Animation, new Point(0, 0));
  84.             }
  85.  
  86.             // Return the array of Bitmap frames
  87.             return Frames;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement