Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Text.RegularExpressions;
- //origional by KipliKipod
- //https://mods.factorio.com/mod/StromTL/discussion/5ac9f59322c7c00009d18179
- namespace Images
- {
- class Program
- {
- private const int SquareSize = 512;
- private const int MaxSideLength = 23000; //max safe square size the Bitmap can be.
- static void Main(string[] args)
- {
- try
- {
- var path = Directory.GetCurrentDirectory();
- //var path = @"C:\FactorioModding\script-output\StromTL";
- var imagesDirectory = path + "\\images\\";
- if (!Directory.Exists(imagesDirectory))
- Directory.CreateDirectory(imagesDirectory);
- Console.WriteLine("Proccecing " + path);
- var directoryPaths = Directory.GetDirectories(path);
- for (int i = 0; i < directoryPaths.Length; i++)
- {
- var directoryPath = directoryPaths[i];
- string[] numbers = Regex.Split(directoryPath, @"\D+");
- if (numbers.Length == 4)
- {
- var index = int.Parse(numbers[1]);
- var width = int.Parse(numbers[2]);
- var height = int.Parse(numbers[3]);
- var imageName = imagesDirectory + index + ".Png";
- if (!File.Exists(imageName))
- {
- Console.WriteLine("Combining: " + directoryPath);
- Combine(directoryPath, width, height, imageName);
- Console.WriteLine("Done");
- }
- else
- Console.WriteLine("Skipping: " + imageName);
- }
- }
- Console.WriteLine();
- Console.WriteLine("All Done");
- }
- catch(Exception ex)
- {
- Console.Write(ex);
- Console.WriteLine();
- }
- Console.WriteLine();
- Console.WriteLine("Press any key to close.");
- Console.ReadKey();
- }
- private static void Combine(string path, int width, int height, string outputFile)
- {
- var maxSize = Math.Max((width * SquareSize), (height * SquareSize));
- decimal scale = Math.Min(1, (new Decimal(MaxSideLength) / maxSize));
- if(scale < 1)
- {
- Console.WriteLine("Scaling down to: " + (Convert.ToInt32(scale * 100)) + "%");
- }
- int squareSize = Math.Min(SquareSize, Convert.ToInt32(SquareSize * scale));
- using (Bitmap bmp = new Bitmap(width * squareSize, height * squareSize))
- {
- using (Graphics g = Graphics.FromImage(bmp))
- {
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- var index = x + y * width;
- var indexImage = Image.FromFile(path + "\\" + index + ".png");
- g.DrawImage(indexImage, x * squareSize, y * squareSize, squareSize, squareSize);
- indexImage.Dispose();
- }
- }
- }
- bmp.Save(outputFile, ImageFormat.Png);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement