Advertisement
Muppet9010

StromTimeLapse c# image merger

Jun 8th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6.  
  7. //origional by KipliKipod
  8. //https://mods.factorio.com/mod/StromTL/discussion/5ac9f59322c7c00009d18179
  9.  
  10. namespace Images
  11. {
  12. class Program
  13. {
  14. private const int SquareSize = 512;
  15. private const int MaxSideLength = 23000; //max safe square size the Bitmap can be.
  16.  
  17. static void Main(string[] args)
  18. {
  19. try
  20. {
  21. var path = Directory.GetCurrentDirectory();
  22. //var path = @"C:\FactorioModding\script-output\StromTL";
  23. var imagesDirectory = path + "\\images\\";
  24. if (!Directory.Exists(imagesDirectory))
  25. Directory.CreateDirectory(imagesDirectory);
  26.  
  27. Console.WriteLine("Proccecing " + path);
  28. var directoryPaths = Directory.GetDirectories(path);
  29. for (int i = 0; i < directoryPaths.Length; i++)
  30. {
  31. var directoryPath = directoryPaths[i];
  32. string[] numbers = Regex.Split(directoryPath, @"\D+");
  33. if (numbers.Length == 4)
  34. {
  35. var index = int.Parse(numbers[1]);
  36. var width = int.Parse(numbers[2]);
  37. var height = int.Parse(numbers[3]);
  38. var imageName = imagesDirectory + index + ".Png";
  39. if (!File.Exists(imageName))
  40. {
  41. Console.WriteLine("Combining: " + directoryPath);
  42. Combine(directoryPath, width, height, imageName);
  43. Console.WriteLine("Done");
  44. }
  45. else
  46. Console.WriteLine("Skipping: " + imageName);
  47. }
  48. }
  49. Console.WriteLine();
  50. Console.WriteLine("All Done");
  51. }
  52. catch(Exception ex)
  53. {
  54. Console.Write(ex);
  55. Console.WriteLine();
  56. }
  57. Console.WriteLine();
  58. Console.WriteLine("Press any key to close.");
  59. Console.ReadKey();
  60. }
  61.  
  62. private static void Combine(string path, int width, int height, string outputFile)
  63. {
  64. var maxSize = Math.Max((width * SquareSize), (height * SquareSize));
  65. decimal scale = Math.Min(1, (new Decimal(MaxSideLength) / maxSize));
  66. if(scale < 1)
  67. {
  68. Console.WriteLine("Scaling down to: " + (Convert.ToInt32(scale * 100)) + "%");
  69. }
  70. int squareSize = Math.Min(SquareSize, Convert.ToInt32(SquareSize * scale));
  71.  
  72. using (Bitmap bmp = new Bitmap(width * squareSize, height * squareSize))
  73. {
  74. using (Graphics g = Graphics.FromImage(bmp))
  75. {
  76. for (int y = 0; y < height; y++)
  77. {
  78. for (int x = 0; x < width; x++)
  79. {
  80. var index = x + y * width;
  81. var indexImage = Image.FromFile(path + "\\" + index + ".png");
  82. g.DrawImage(indexImage, x * squareSize, y * squareSize, squareSize, squareSize);
  83. indexImage.Dispose();
  84. }
  85. }
  86. }
  87.  
  88. bmp.Save(outputFile, ImageFormat.Png);
  89. }
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement