2
0

DynamicImageHelpers.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Emby.Drawing.ImageMagick;
  2. using MediaBrowser.Common.IO;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. namespace Emby.Drawing.GDI
  10. {
  11. public static class DynamicImageHelpers
  12. {
  13. public static void CreateThumbCollage(List<string> files,
  14. IFileSystem fileSystem,
  15. string file,
  16. int width,
  17. int height)
  18. {
  19. const int numStrips = 4;
  20. files = StripCollageBuilder.ProjectPaths(files, numStrips).ToList();
  21. const int rows = 1;
  22. int cols = numStrips;
  23. int cellWidth = 2 * (width / 3);
  24. int cellHeight = height;
  25. var index = 0;
  26. using (var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
  27. {
  28. using (var graphics = Graphics.FromImage(img))
  29. {
  30. graphics.CompositingQuality = CompositingQuality.HighQuality;
  31. graphics.SmoothingMode = SmoothingMode.HighQuality;
  32. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  33. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  34. graphics.CompositingMode = CompositingMode.SourceCopy;
  35. for (var row = 0; row < rows; row++)
  36. {
  37. for (var col = 0; col < cols; col++)
  38. {
  39. var x = col * (cellWidth / 2);
  40. var y = row * cellHeight;
  41. if (files.Count > index)
  42. {
  43. using (var fileStream = fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
  44. {
  45. using (var memoryStream = new MemoryStream())
  46. {
  47. fileStream.CopyTo(memoryStream);
  48. memoryStream.Position = 0;
  49. using (var imgtemp = Image.FromStream(memoryStream, true, false))
  50. {
  51. graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
  52. }
  53. }
  54. }
  55. }
  56. index++;
  57. }
  58. }
  59. img.Save(file);
  60. }
  61. }
  62. }
  63. public static void CreateSquareCollage(List<string> files,
  64. IFileSystem fileSystem,
  65. string file,
  66. int width,
  67. int height)
  68. {
  69. files = StripCollageBuilder.ProjectPaths(files, 4).ToList();
  70. const int rows = 2;
  71. const int cols = 2;
  72. int singleSize = width / 2;
  73. var index = 0;
  74. using (var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
  75. {
  76. using (var graphics = Graphics.FromImage(img))
  77. {
  78. graphics.CompositingQuality = CompositingQuality.HighQuality;
  79. graphics.SmoothingMode = SmoothingMode.HighQuality;
  80. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  81. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  82. graphics.CompositingMode = CompositingMode.SourceCopy;
  83. for (var row = 0; row < rows; row++)
  84. {
  85. for (var col = 0; col < cols; col++)
  86. {
  87. var x = col * singleSize;
  88. var y = row * singleSize;
  89. using (var fileStream = fileSystem.GetFileStream(files[index], FileMode.Open, FileAccess.Read, FileShare.Read, true))
  90. {
  91. using (var memoryStream = new MemoryStream())
  92. {
  93. fileStream.CopyTo(memoryStream);
  94. memoryStream.Position = 0;
  95. using (var imgtemp = Image.FromStream(memoryStream, true, false))
  96. {
  97. graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
  98. }
  99. }
  100. }
  101. index++;
  102. }
  103. }
  104. img.Save(file);
  105. }
  106. }
  107. }
  108. private static Stream GetStream(Image image)
  109. {
  110. var ms = new MemoryStream();
  111. image.Save(ms, ImageFormat.Png);
  112. ms.Position = 0;
  113. return ms;
  114. }
  115. }
  116. }