Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Windows.Forms;
- namespace PokemonSpriteConverter
- {
- class Program
- {
- private const string DIR_SPRITES = @"\spritesToConvert";
- private const string DIR_CONVERTED = @"\convertedSprites\";
- public static void Main(string[] args)
- {
- if (!Directory.Exists(Application.StartupPath + DIR_SPRITES))
- Directory.CreateDirectory(Application.StartupPath + DIR_SPRITES);
- if (!Directory.Exists(Application.StartupPath + DIR_CONVERTED))
- Directory.CreateDirectory(Application.StartupPath + DIR_CONVERTED);
- start();
- }
- private static void start()
- {
- Console.WriteLine("Welcome to the Pokemon Sprite Converter. Type 'start' to begin converting from the " + DIR_SPRITES + " folder to the " + DIR_CONVERTED + " folder.");
- string s = Console.ReadLine();
- if (s.Equals("start"))
- {
- DirectoryInfo dirInfo = new DirectoryInfo(Application.StartupPath + DIR_SPRITES);
- Bitmap gif = null;
- Bitmap[] frames = null;
- Bitmap converted = null;
- foreach (FileInfo file in dirInfo.GetFiles())
- {
- gif = new Bitmap(file.FullName);
- frames = ParseFrames(gif);
- converted = new Bitmap(frames[0].Width * frames.Length, frames[0].Height);
- for (int i = 0; i < frames.Length; i++)
- using (var g = Graphics.FromImage(converted))
- g.DrawImage(frames[i], new Rectangle(i * frames[i].Width, 0, frames[i].Width, frames[i].Height));
- converted.Save(Application.StartupPath + @"\convertedSprites\" + file.Name.Split('.')[0] + ".png");
- }
- }
- else
- {
- start();
- return;
- }
- Console.WriteLine("Conversion complete! Press any key to continue...");
- Console.ReadLine();
- }
- // Credit: Answer from 'Neoheurist' in http://stackoverflow.com/questions/1712708/how-to-split-an-animated-gif-in-net
- private static Bitmap[] ParseFrames(Bitmap Animation)
- {
- // Get the number of animation frames to copy into a Bitmap array
- int Length = Animation.GetFrameCount(FrameDimension.Time);
- // Allocate a Bitmap array to hold individual frames from the animation
- Bitmap[] Frames = new Bitmap[Length];
- // Copy the animation Bitmap frames into the Bitmap array
- for (int Index = 0; Index < Length; Index++)
- {
- // Set the current frame within the animation to be copied into the Bitmap array element
- Animation.SelectActiveFrame(FrameDimension.Time, Index);
- // Create a new Bitmap element within the Bitmap array in which to copy the next fram
- Frames[Index] = new Bitmap(Animation.Size.Width, Animation.Size.Height);
- // Copy the current animation frame into the new Bitmap array element
- Graphics.FromImage(Frames[Index]).DrawImage(Animation, new Point(0, 0));
- }
- // Return the array of Bitmap frames
- return Frames;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement