DrawingUtils.cs 5.3 KB

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