DynamicImageHelpers.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 CommonIO;
  7. namespace Emby.Drawing.GDI
  8. {
  9. public static class DynamicImageHelpers
  10. {
  11. public static void CreateThumbCollage(List<string> files,
  12. IFileSystem fileSystem,
  13. string file,
  14. int width,
  15. int height)
  16. {
  17. const int numStrips = 4;
  18. files = ImageHelpers.ProjectPaths(files, numStrips);
  19. const int rows = 1;
  20. int cols = numStrips;
  21. int cellWidth = 2 * (width / 3);
  22. int cellHeight = height;
  23. var index = 0;
  24. using (var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
  25. {
  26. using (var graphics = Graphics.FromImage(img))
  27. {
  28. graphics.CompositingQuality = CompositingQuality.HighQuality;
  29. graphics.SmoothingMode = SmoothingMode.HighQuality;
  30. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  31. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  32. // SourceCopy causes the image to be blank in OSX
  33. //graphics.CompositingMode = CompositingMode.SourceCopy;
  34. for (var row = 0; row < rows; row++)
  35. {
  36. for (var col = 0; col < cols; col++)
  37. {
  38. var x = col * (cellWidth / 2);
  39. var y = row * cellHeight;
  40. if (files.Count > index)
  41. {
  42. using (var imgtemp = Image.FromFile(files[index]))
  43. {
  44. graphics.DrawImage(imgtemp, x, y, cellWidth, cellHeight);
  45. }
  46. }
  47. index++;
  48. }
  49. }
  50. img.Save(file);
  51. }
  52. }
  53. }
  54. public static void CreateSquareCollage(List<string> files,
  55. IFileSystem fileSystem,
  56. string file,
  57. int width,
  58. int height)
  59. {
  60. files = ImageHelpers.ProjectPaths(files, 4);
  61. const int rows = 2;
  62. const int cols = 2;
  63. int singleSize = width / 2;
  64. var index = 0;
  65. using (var img = new Bitmap(width, height, PixelFormat.Format32bppPArgb))
  66. {
  67. using (var graphics = Graphics.FromImage(img))
  68. {
  69. graphics.CompositingQuality = CompositingQuality.HighQuality;
  70. graphics.SmoothingMode = SmoothingMode.HighQuality;
  71. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  72. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  73. // SourceCopy causes the image to be blank in OSX
  74. //graphics.CompositingMode = CompositingMode.SourceCopy;
  75. for (var row = 0; row < rows; row++)
  76. {
  77. for (var col = 0; col < cols; col++)
  78. {
  79. var x = col * singleSize;
  80. var y = row * singleSize;
  81. using (var imgtemp = Image.FromFile(files[index]))
  82. {
  83. graphics.DrawImage(imgtemp, x, y, singleSize, singleSize);
  84. }
  85. index++;
  86. }
  87. }
  88. img.Save(file);
  89. }
  90. }
  91. }
  92. }
  93. }