/// <summary>
        /// Seperates a SpriteSheet into individual images.
        /// </summary>
        /// <param name="spritesheet_filename">Path and file name of existing SpriteSheet to load</param>
        /// <param name="image_pattern">object containing tiling pattern information.</param>
        /// <param name="file_name">Name of output file. Do not include the file type or path.</param>
        /// <param name="output_directory">Path to output file directory. Will be created if it does not exist.</param>
        /// <param name="output_file_type">ImageFormat enumeration of file type to generate.</param>
        /// <param name="update_progress_counter">delegate callback to some external counter</param>
        /// <returns>bool flag indicating if any errors occured while processing sprite sheet.</returns>
        public static bool SeperateSpriteSheet(string spritesheet_filename, Point image_pattern, string output_filename, string output_directory, string output_file_type, UpdateProgressBar update_progress_counter)
        {
            ImageFormat image_format = ParseOutputType(output_file_type);

            try
            {
                if (!Directory.Exists(output_directory))
                {
                    Directory.CreateDirectory(output_directory);
                }

                // Load spritsheet - why so filestream?
                //file_stream = new FileStream(spritesheet_filename, FileMode.Open);
                //Bitmap sprite_sheet = new Bitmap(file_stream);
                //file_stream.Close();

                Bitmap sprite_sheet = new Bitmap(spritesheet_filename);

                //List<Bitmap> output_file_list = new List<Bitmap>();
                Bitmap output_file;

                Point image_size    = new Point(sprite_sheet.Width / image_pattern.X, sprite_sheet.Height / image_pattern.Y);
                Point current_image = new Point(0, 0);

                // Set up output buffer
                int count = (image_pattern.X * image_pattern.Y);

                for (int i = 0; i < count; i++)
                {
                    output_file = new Bitmap(image_size.X, image_size.Y);

                    // Copy images
                    int x, y;

                    for (x = 0; x < image_size.X; x++)
                    {
                        for (y = 0; y < image_size.Y; y++)
                        {
                            Color color = sprite_sheet.GetPixel(image_size.X * current_image.X + x, image_size.Y * current_image.Y + y);
                            output_file.SetPixel(x, y, color);
                        }
                    }

                    // Update where we are on spritesheet
                    current_image.X += 1;

                    if (current_image.X == image_pattern.X)
                    {
                        current_image.X  = 0;
                        current_image.Y += 1;
                    }

                    if (current_image.Y == image_pattern.Y)
                    {
                        current_image.Y = 0;
                    }

                    string file_counter = (i + 1).ToString();

                    while (file_counter.Length < 4)
                    {
                        file_counter = "0" + file_counter;
                    }

                    string filename = output_filename + "(" + file_counter + ")." + image_format.ToString().ToLower();

                    using (FileStream file_stream = new FileStream(output_directory + "\\" + filename, FileMode.Create))
                        output_file.Save(file_stream, image_format);

                    if (update_progress_counter != null)
                    {
                        update_progress_counter.Invoke();
                    }
                }
            }
            catch
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Creates a sprite sheet from a list of bitmap file names.
        /// </summary>
        /// <param name="file_names">list object containing filenames of images to include in spritesheet.</param>
        /// <param name="image_pattern">object containing tiling pattern information.</param>
        /// <param name="background_color">background color of spritesheet. Only important if tiling pattern doesn't
        /// completely cover spritesheet. Ignored if parameter is set transparent.</param>
        /// <param name="mask_color">color of overlay mask to apply after spritesheet is built. No mask is set if
        /// parameter is set to transparent.</param>
        /// <param name="file_name">Name of output file. Do not include the file type or path.</param>
        /// <param name="output_directory">Path to output file directory. Will be created if it does not exist.</param>
        /// <param name="output_file_type">ImageFormat enumeration of file type to generate.</param>
        /// <param name="update_progress_counter">A delegate callback to some external counter.</param>
        /// <returns>bool flag indicating if any errors occured while processing sprite sheet.</returns>
        public static bool CreateSpriteSheet(List <string> image_file_names, Point image_pattern, KnownColor background_color, KnownColor mask_color, string raw_file_name, string output_directory, string raw_image_format, UpdateProgressBar update_progress_counter)
        {
            ImageFormat parsed_image_format = ParseOutputType(raw_image_format);

            try
            {
                // Load up images
                List <Bitmap> bitmaps = new List <Bitmap>();

                foreach (string image_file_name in image_file_names)
                {
                    using (FileStream file_stream = new FileStream(image_file_name, FileMode.Open))
                        bitmaps.Add(new Bitmap(file_stream));
                }

                Point  image_size   = GetLargestImageDimensions(bitmaps);
                Bitmap sprite_sheet = new Bitmap(image_size.X * image_pattern.X, image_size.Y * image_pattern.Y);

                // WAIT A F*****G SECOND, does this even do anything?
                //Set Background color
                if (background_color != KnownColor.Transparent)
                {
                    Graphics graphics_obj = Graphics.FromImage(sprite_sheet);
                    graphics_obj.Clear(Color.FromKnownColor(background_color));
                    graphics_obj.Dispose();
                }

                // Copy images
                int   x, y, z;
                Point current_image = new Point(0, 0);

                for (z = 0; z < bitmaps.Count; z++)
                {
                    for (x = 0; x < image_size.X; x++)
                    {
                        for (y = 0; y < image_size.Y; y++)
                        {
                            Color color = bitmaps[z].GetPixel(x, y);
                            sprite_sheet.SetPixel(image_size.X * current_image.X + x, image_size.Y * current_image.Y + y, color);
                        }
                    }

                    // Update where we are on spritesheet
                    current_image.X += 1;

                    if (current_image.X == image_pattern.X)
                    {
                        current_image.X  = 0;
                        current_image.Y += 1;
                    }

                    if (current_image.Y == image_pattern.Y)
                    {
                        current_image.Y = 0;
                    }

                    if (update_progress_counter != null)
                    {
                        update_progress_counter.Invoke();
                    }
                }

                // set color mask
                if (mask_color != KnownColor.Transparent)
                {
                    sprite_sheet.MakeTransparent(Color.FromKnownColor(mask_color));
                }

                // save file
                if (!Directory.Exists(output_directory))
                {
                    Directory.CreateDirectory(output_directory);
                }

                string file_path;

                if (output_directory.LastIndexOf('\\') == output_directory.Length - 1)
                {
                    file_path = output_directory + raw_file_name;
                }
                else
                {
                    file_path = output_directory + "\\" + raw_file_name;
                }

                int    file_number      = 0;
                string file_name        = BuildFileName(raw_file_name, image_size, image_file_names.Count, output_directory, parsed_image_format);
                string output_file_name = file_name.Replace(FILE_NUMBER, string.Empty);

                // first time we try without the file number
                while (File.Exists(output_file_name))
                {
                    ++file_number;
                    output_file_name = file_name.Replace(FILE_NUMBER, "(" + file_number.ToString() + ")");
                }

                using (FileStream file_stream = new FileStream(output_directory + "\\" + output_file_name, FileMode.Create))
                    sprite_sheet.Save(file_stream, parsed_image_format);
            }
            catch
            {
                return(false);
            }

            return(true);
        }
Example #3
0
 private void CalculateCompletedHeader()
 {
     CompletedHeader   = $"Completed { Items.Count(x => x.Completed)}/{ Items.Count}";
     CompletedProgress = (double)Items.Count(x => x.Completed) / (double)Items.Count;
     UpdateProgressBar?.Invoke(this, CompletedProgress);
 }