DrawingUtils.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. namespace MediaBrowser.Model.Drawing
  3. {
  4. /// <summary>
  5. /// Class DrawingUtils.
  6. /// </summary>
  7. public static class DrawingUtils
  8. {
  9. /// <summary>
  10. /// Resizes a set of dimensions.
  11. /// </summary>
  12. /// <param name="size">The original size object.</param>
  13. /// <param name="width">A new fixed width, if desired.</param>
  14. /// <param name="height">A new fixed height, if desired.</param>
  15. /// <param name="maxWidth">A max fixed width, if desired.</param>
  16. /// <param name="maxHeight">A max fixed height, if desired.</param>
  17. /// <returns>A new size object.</returns>
  18. public static ImageDimensions Resize(
  19. ImageDimensions size,
  20. int width,
  21. int height,
  22. int maxWidth,
  23. int maxHeight)
  24. {
  25. int newWidth = size.Width;
  26. int newHeight = size.Height;
  27. if (width > 0 && height > 0)
  28. {
  29. newWidth = width;
  30. newHeight = height;
  31. }
  32. else if (height > 0)
  33. {
  34. newWidth = GetNewWidth(newHeight, newWidth, height);
  35. newHeight = height;
  36. }
  37. else if (width > 0)
  38. {
  39. newHeight = GetNewHeight(newHeight, newWidth, width);
  40. newWidth = width;
  41. }
  42. if (maxHeight > 0 && maxHeight < newHeight)
  43. {
  44. newWidth = GetNewWidth(newHeight, newWidth, maxHeight);
  45. newHeight = maxHeight;
  46. }
  47. if (maxWidth > 0 && maxWidth < newWidth)
  48. {
  49. newHeight = GetNewHeight(newHeight, newWidth, maxWidth);
  50. newWidth = maxWidth;
  51. }
  52. return new ImageDimensions(newWidth, newHeight);
  53. }
  54. /// <summary>
  55. /// Scale down to fill box.
  56. /// Returns original size if both width and height are null or zero.
  57. /// </summary>
  58. /// <param name="size">The original size object.</param>
  59. /// <param name="fillWidth">A new fixed width, if desired.</param>
  60. /// <param name="fillHeight">A new fixed height, if desired.</param>
  61. /// <returns>A new size object or size.</returns>
  62. public static ImageDimensions ResizeFill(
  63. ImageDimensions size,
  64. int? fillWidth,
  65. int? fillHeight)
  66. {
  67. // Return original size if input is invalid.
  68. if ((fillWidth is null || fillWidth == 0)
  69. && (fillHeight is null || fillHeight == 0))
  70. {
  71. return size;
  72. }
  73. if (fillWidth is null || fillWidth == 0)
  74. {
  75. fillWidth = 1;
  76. }
  77. if (fillHeight is null || fillHeight == 0)
  78. {
  79. fillHeight = 1;
  80. }
  81. double widthRatio = size.Width / (double)fillWidth;
  82. double heightRatio = size.Height / (double)fillHeight;
  83. double scaleRatio = Math.Min(widthRatio, heightRatio);
  84. // Clamp to current size.
  85. if (scaleRatio < 1)
  86. {
  87. return size;
  88. }
  89. int newWidth = Convert.ToInt32(Math.Ceiling(size.Width / scaleRatio));
  90. int newHeight = Convert.ToInt32(Math.Ceiling(size.Height / scaleRatio));
  91. return new ImageDimensions(newWidth, newHeight);
  92. }
  93. /// <summary>
  94. /// Gets the new width.
  95. /// </summary>
  96. /// <param name="currentHeight">Height of the current.</param>
  97. /// <param name="currentWidth">Width of the current.</param>
  98. /// <param name="newHeight">The new height.</param>
  99. /// <returns>The new width.</returns>
  100. private static int GetNewWidth(int currentHeight, int currentWidth, int newHeight)
  101. => Convert.ToInt32((double)newHeight / currentHeight * currentWidth);
  102. /// <summary>
  103. /// Gets the new height.
  104. /// </summary>
  105. /// <param name="currentHeight">Height of the current.</param>
  106. /// <param name="currentWidth">Width of the current.</param>
  107. /// <param name="newWidth">The new width.</param>
  108. /// <returns>System.Double.</returns>
  109. private static int GetNewHeight(int currentHeight, int currentWidth, int newWidth)
  110. => Convert.ToInt32((double)newWidth / currentWidth * currentHeight);
  111. }
  112. }