DrawingUtils.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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
  19. {
  20. Width = currentWidth,
  21. Height = currentHeight
  22. }, scaleFactor);
  23. }
  24. /// <summary>
  25. /// Resizes a set of dimensions
  26. /// </summary>
  27. /// <param name="size">The size.</param>
  28. /// <param name="scaleFactor">The scale factor.</param>
  29. /// <returns>ImageSize.</returns>
  30. public static ImageSize Scale(ImageSize size, double scaleFactor)
  31. {
  32. var newWidth = size.Width * scaleFactor;
  33. return Resize(size.Width, size.Height, newWidth, null, null, null);
  34. }
  35. /// <summary>
  36. /// Resizes a set of dimensions
  37. /// </summary>
  38. /// <param name="currentWidth">Width of the current.</param>
  39. /// <param name="currentHeight">Height of the current.</param>
  40. /// <param name="width">The width.</param>
  41. /// <param name="height">The height.</param>
  42. /// <param name="maxWidth">A max fixed width, if desired</param>
  43. /// <param name="maxHeight">A max fixed height, if desired</param>
  44. /// <returns>ImageSize.</returns>
  45. public static ImageSize Resize(double currentWidth,
  46. double currentHeight,
  47. double? width,
  48. double? height,
  49. double? maxWidth,
  50. double? maxHeight)
  51. {
  52. return Resize(new ImageSize
  53. {
  54. Width = currentWidth,
  55. Height = currentHeight
  56. }, width, height, maxWidth, maxHeight);
  57. }
  58. /// <summary>
  59. /// Resizes a set of dimensions
  60. /// </summary>
  61. /// <param name="size">The original size object</param>
  62. /// <param name="width">A new fixed width, if desired</param>
  63. /// <param name="height">A new fixed height, if desired</param>
  64. /// <param name="maxWidth">A max fixed width, if desired</param>
  65. /// <param name="maxHeight">A max fixed height, if desired</param>
  66. /// <returns>A new size object</returns>
  67. public static ImageSize Resize(ImageSize size,
  68. double? width,
  69. double? height,
  70. double? maxWidth,
  71. double? maxHeight)
  72. {
  73. double newWidth = size.Width;
  74. double newHeight = size.Height;
  75. if (width.HasValue && height.HasValue)
  76. {
  77. newWidth = width.Value;
  78. newHeight = height.Value;
  79. }
  80. else if (height.HasValue)
  81. {
  82. newWidth = GetNewWidth(newHeight, newWidth, height.Value);
  83. newHeight = height.Value;
  84. }
  85. else if (width.HasValue)
  86. {
  87. newHeight = GetNewHeight(newHeight, newWidth, width.Value);
  88. newWidth = width.Value;
  89. }
  90. if (maxHeight.HasValue && maxHeight.Value < newHeight)
  91. {
  92. newWidth = GetNewWidth(newHeight, newWidth, maxHeight.Value);
  93. newHeight = maxHeight.Value;
  94. }
  95. if (maxWidth.HasValue && maxWidth.Value < newWidth)
  96. {
  97. newHeight = GetNewHeight(newHeight, newWidth, maxWidth.Value);
  98. newWidth = maxWidth.Value;
  99. }
  100. return new ImageSize { Width = newWidth, Height = newHeight };
  101. }
  102. /// <summary>
  103. /// Gets the new width.
  104. /// </summary>
  105. /// <param name="currentHeight">Height of the current.</param>
  106. /// <param name="currentWidth">Width of the current.</param>
  107. /// <param name="newHeight">The new height.</param>
  108. /// <returns>System.Double.</returns>
  109. private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight)
  110. {
  111. var scaleFactor = newHeight;
  112. scaleFactor /= currentHeight;
  113. scaleFactor *= currentWidth;
  114. return scaleFactor;
  115. }
  116. /// <summary>
  117. /// Gets the new height.
  118. /// </summary>
  119. /// <param name="currentHeight">Height of the current.</param>
  120. /// <param name="currentWidth">Width of the current.</param>
  121. /// <param name="newWidth">The new width.</param>
  122. /// <returns>System.Double.</returns>
  123. private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth)
  124. {
  125. var scaleFactor = newWidth;
  126. scaleFactor /= currentWidth;
  127. scaleFactor *= currentHeight;
  128. return scaleFactor;
  129. }
  130. }
  131. /// <summary>
  132. /// Struct ImageSize
  133. /// </summary>
  134. public struct ImageSize
  135. {
  136. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  137. private double _height;
  138. private double _width;
  139. /// <summary>
  140. /// Gets or sets the height.
  141. /// </summary>
  142. /// <value>The height.</value>
  143. public double Height
  144. {
  145. get
  146. {
  147. return _height;
  148. }
  149. set
  150. {
  151. _height = value;
  152. }
  153. }
  154. /// <summary>
  155. /// Gets or sets the width.
  156. /// </summary>
  157. /// <value>The width.</value>
  158. public double Width
  159. {
  160. get { return _width; }
  161. set { _width = value; }
  162. }
  163. public bool Equals(ImageSize size)
  164. {
  165. return Width.Equals(size.Width) && Height.Equals(size.Height);
  166. }
  167. public override string ToString()
  168. {
  169. return string.Format("{0}-{1}", Width, Height);
  170. }
  171. public ImageSize(string value)
  172. {
  173. _width = 0;
  174. _height = 0;
  175. ParseValue(value);
  176. }
  177. private void ParseValue(string value)
  178. {
  179. if (!string.IsNullOrEmpty(value))
  180. {
  181. string[] parts = value.Split('-');
  182. if (parts.Length == 2)
  183. {
  184. double val;
  185. if (double.TryParse(parts[0], NumberStyles.Any, UsCulture, out val))
  186. {
  187. _width = val;
  188. }
  189. if (double.TryParse(parts[1], NumberStyles.Any, UsCulture, out val))
  190. {
  191. _height = val;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }