DrawingUtils.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Globalization;
  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. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  120. private double _height;
  121. private double _width;
  122. /// <summary>
  123. /// Gets or sets the height.
  124. /// </summary>
  125. /// <value>The height.</value>
  126. public double Height
  127. {
  128. get
  129. {
  130. return _height;
  131. }
  132. set
  133. {
  134. _height = value;
  135. }
  136. }
  137. /// <summary>
  138. /// Gets or sets the width.
  139. /// </summary>
  140. /// <value>The width.</value>
  141. public double Width
  142. {
  143. get { return _width; }
  144. set { _width = value; }
  145. }
  146. public bool Equals(ImageSize size)
  147. {
  148. return Width.Equals(size.Width) && Height.Equals(size.Height);
  149. }
  150. public override string ToString()
  151. {
  152. return string.Format("{0}-{1}", Width, Height);
  153. }
  154. public ImageSize(string value)
  155. {
  156. _width = 0;
  157. _height = 0;
  158. ParseValue(value);
  159. }
  160. private void ParseValue(string value)
  161. {
  162. if (!string.IsNullOrEmpty(value))
  163. {
  164. var parts = value.Split('-');
  165. if (parts.Length == 2)
  166. {
  167. double val;
  168. if (double.TryParse(parts[0], NumberStyles.Any, UsCulture, out val))
  169. {
  170. _width = val;
  171. }
  172. if (double.TryParse(parts[1], NumberStyles.Any, UsCulture, out val))
  173. {
  174. _height = val;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }