DrawingUtils.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. namespace MediaBrowser.Model.Drawing
  2. {
  3. /// <summary>
  4. /// Class DrawingUtils
  5. /// </summary>
  6. public static class DrawingUtils
  7. {
  8. /// <summary>
  9. /// Resizes a set of dimensions
  10. /// </summary>
  11. /// <param name="currentWidth">Width of the current.</param>
  12. /// <param name="currentHeight">Height of the current.</param>
  13. /// <param name="scaleFactor">The scale factor.</param>
  14. /// <returns>ImageSize.</returns>
  15. public static ImageSize Scale(double currentWidth, double currentHeight, double scaleFactor)
  16. {
  17. return Scale(new ImageSize
  18. {
  19. Width = currentWidth,
  20. Height = currentHeight
  21. }, scaleFactor);
  22. }
  23. /// <summary>
  24. /// Resizes a set of dimensions
  25. /// </summary>
  26. /// <param name="size">The size.</param>
  27. /// <param name="scaleFactor">The scale factor.</param>
  28. /// <returns>ImageSize.</returns>
  29. public static ImageSize Scale(ImageSize size, double scaleFactor)
  30. {
  31. double newWidth = size.Width * scaleFactor;
  32. return Resize(size.Width, size.Height, newWidth, null, null, null);
  33. }
  34. /// <summary>
  35. /// Resizes a set of dimensions
  36. /// </summary>
  37. /// <param name="currentWidth">Width of the current.</param>
  38. /// <param name="currentHeight">Height of the current.</param>
  39. /// <param name="width">The width.</param>
  40. /// <param name="height">The height.</param>
  41. /// <param name="maxWidth">A max fixed width, if desired</param>
  42. /// <param name="maxHeight">A max fixed height, if desired</param>
  43. /// <returns>ImageSize.</returns>
  44. public static ImageSize Resize(double currentWidth,
  45. double currentHeight,
  46. double? width,
  47. double? height,
  48. double? maxWidth,
  49. double? maxHeight)
  50. {
  51. return Resize(new ImageSize
  52. {
  53. Width = currentWidth,
  54. Height = currentHeight
  55. }, width, height, maxWidth, maxHeight);
  56. }
  57. /// <summary>
  58. /// Resizes a set of dimensions
  59. /// </summary>
  60. /// <param name="size">The original size object</param>
  61. /// <param name="width">A new fixed width, if desired</param>
  62. /// <param name="height">A new fixed height, if desired</param>
  63. /// <param name="maxWidth">A max fixed width, if desired</param>
  64. /// <param name="maxHeight">A max fixed height, if desired</param>
  65. /// <returns>A new size object</returns>
  66. public static ImageSize Resize(ImageSize size,
  67. double? width,
  68. double? height,
  69. double? maxWidth,
  70. double? maxHeight)
  71. {
  72. double newWidth = size.Width;
  73. double newHeight = size.Height;
  74. if (width.HasValue && height.HasValue)
  75. {
  76. newWidth = width.Value;
  77. newHeight = height.Value;
  78. }
  79. else if (height.HasValue)
  80. {
  81. newWidth = GetNewWidth(newHeight, newWidth, height.Value);
  82. newHeight = height.Value;
  83. }
  84. else if (width.HasValue)
  85. {
  86. newHeight = GetNewHeight(newHeight, newWidth, width.Value);
  87. newWidth = width.Value;
  88. }
  89. if (maxHeight.HasValue && maxHeight.Value < newHeight)
  90. {
  91. newWidth = GetNewWidth(newHeight, newWidth, maxHeight.Value);
  92. newHeight = maxHeight.Value;
  93. }
  94. if (maxWidth.HasValue && maxWidth.Value < newWidth)
  95. {
  96. newHeight = GetNewHeight(newHeight, newWidth, maxWidth.Value);
  97. newWidth = maxWidth.Value;
  98. }
  99. return new ImageSize { Width = newWidth, Height = newHeight };
  100. }
  101. /// <summary>
  102. /// Gets the new width.
  103. /// </summary>
  104. /// <param name="currentHeight">Height of the current.</param>
  105. /// <param name="currentWidth">Width of the current.</param>
  106. /// <param name="newHeight">The new height.</param>
  107. /// <returns>System.Double.</returns>
  108. private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight)
  109. {
  110. double scaleFactor = newHeight;
  111. scaleFactor /= currentHeight;
  112. scaleFactor *= currentWidth;
  113. return scaleFactor;
  114. }
  115. /// <summary>
  116. /// Gets the new height.
  117. /// </summary>
  118. /// <param name="currentHeight">Height of the current.</param>
  119. /// <param name="currentWidth">Width of the current.</param>
  120. /// <param name="newWidth">The new width.</param>
  121. /// <returns>System.Double.</returns>
  122. private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth)
  123. {
  124. double scaleFactor = newWidth;
  125. scaleFactor /= currentWidth;
  126. scaleFactor *= currentHeight;
  127. return scaleFactor;
  128. }
  129. }
  130. }