2
0

DynamicImageHelpers.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using MediaBrowser.Common.IO;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Model.IO;
  9. namespace Emby.Drawing.Net
  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 = ImageHelpers.ProjectPaths(files, numStrips);
  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. // SourceCopy causes the image to be blank in OSX
  35. //graphics.CompositingMode = CompositingMode.SourceCopy;
  36. for (var row = 0; row < rows; row++)
  37. {
  38. for (var col = 0; col < cols; col++)
  39. {
  40. var x = col * (cellWidth / 2);
  41. var y = row * cellHeight;
  42. if (files.Count > index)
  43. {
  44. using (var imgtemp = Image.FromFile(files[index]))
  45. {
  46. graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
  47. }
  48. }
  49. index++;
  50. }
  51. }
  52. img.Save(file);
  53. }
  54. }
  55. }
  56. public static void CreateSquareCollage(List<string> files,
  57. IFileSystem fileSystem,
  58. string file,
  59. int width,
  60. int height)
  61. {
  62. files = ImageHelpers.ProjectPaths(files, 4);
  63. const int rows = 2;
  64. const int cols = 2;
  65. int singleSize = width / 2;
  66. var index = 0;
  67. using (var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
  68. {
  69. using (var graphics = Graphics.FromImage(img))
  70. {
  71. graphics.CompositingQuality = CompositingQuality.HighQuality;
  72. graphics.SmoothingMode = SmoothingMode.HighQuality;
  73. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  74. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  75. // SourceCopy causes the image to be blank in OSX
  76. //graphics.CompositingMode = CompositingMode.SourceCopy;
  77. for (var row = 0; row < rows; row++)
  78. {
  79. for (var col = 0; col < cols; col++)
  80. {
  81. var x = col * singleSize;
  82. var y = row * singleSize;
  83. using (var imgtemp = Image.FromFile(files[index]))
  84. {
  85. graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
  86. }
  87. index++;
  88. }
  89. }
  90. img.Save(file);
  91. }
  92. }
  93. }
  94. }
  95. }