Advertisement
Guest User

Images

a guest
Apr 20th, 2018
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 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. namespace Images
  8. {
  9. class Program
  10. {
  11. private const int SquareSize = 512;
  12.  
  13. static void Main(string[] args)
  14. {
  15. var path = Directory.GetCurrentDirectory();
  16. var imagesDirectory = path + "\\images\\";
  17. if (!Directory.Exists(imagesDirectory))
  18. Directory.CreateDirectory(imagesDirectory);
  19.  
  20. Console.WriteLine("Proccecing " + path);
  21. var directoryPaths = Directory.GetDirectories(path);
  22. for (int i = 0; i < directoryPaths.Length; i++)
  23. {
  24. var directoryPath = directoryPaths[i];
  25. string[] numbers = Regex.Split(directoryPath, @"\D+");
  26. if (numbers.Length == 4)
  27. {
  28. var index = int.Parse(numbers[1]);
  29. var width = int.Parse(numbers[2]);
  30. var height = int.Parse(numbers[3]);
  31. var imageName = imagesDirectory + index + ".Jpeg";
  32. if (!File.Exists(imageName))
  33. {
  34. Console.WriteLine("Combining: " + directoryPath);
  35. Combine(directoryPath, width, height, imageName);
  36. Console.WriteLine("Done");
  37. }
  38. else
  39. Console.WriteLine("Skipping: " + imageName);
  40. }
  41. }
  42. Console.WriteLine("All Done");
  43. }
  44.  
  45. private static void Combine(string path,int width,int height,string outputFile)
  46. {
  47. using (Bitmap bmp = new Bitmap(width * SquareSize , height * SquareSize))
  48. {
  49. using (Graphics g = Graphics.FromImage(bmp))
  50. {
  51. for (int y = 0; y < height; y++)
  52. {
  53. for (int x = 0; x < width; x++)
  54. {
  55. var index = x + y * width;
  56. var indexImage = Image.FromFile(path+ "\\" + index + ".png");
  57. g.DrawImage(indexImage, x * SquareSize, y * SquareSize, SquareSize, SquareSize);
  58. indexImage.Dispose();
  59. }
  60. }
  61. }
  62.  
  63. bmp.Save(outputFile, ImageFormat.Jpeg);
  64. }
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement