DrawingUtils.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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="size">The original size object</param>
  12. /// <param name="width">A new fixed width, if desired</param>
  13. /// <param name="height">A new fixed height, if desired</param>
  14. /// <param name="maxWidth">A max fixed width, if desired</param>
  15. /// <param name="maxHeight">A max fixed height, if desired</param>
  16. /// <returns>A new size object</returns>
  17. public static ImageSize Resize(ImageSize size,
  18. double width,
  19. double height,
  20. double maxWidth,
  21. double maxHeight)
  22. {
  23. double newWidth = size.Width;
  24. double newHeight = size.Height;
  25. if (width > 0 && height > 0)
  26. {
  27. newWidth = width;
  28. newHeight = height;
  29. }
  30. else if (height > 0)
  31. {
  32. newWidth = GetNewWidth(newHeight, newWidth, height);
  33. newHeight = height;
  34. }
  35. else if (width > 0)
  36. {
  37. newHeight = GetNewHeight(newHeight, newWidth, width);
  38. newWidth = width;
  39. }
  40. if (maxHeight > 0 && maxHeight < newHeight)
  41. {
  42. newWidth = GetNewWidth(newHeight, newWidth, maxHeight);
  43. newHeight = maxHeight;
  44. }
  45. if (maxWidth > 0 && maxWidth < newWidth)
  46. {
  47. newHeight = GetNewHeight(newHeight, newWidth, maxWidth);
  48. newWidth = maxWidth;
  49. }
  50. return new ImageSize { Width = newWidth, Height = newHeight };
  51. }
  52. /// <summary>
  53. /// Gets the new width.
  54. /// </summary>
  55. /// <param name="currentHeight">Height of the current.</param>
  56. /// <param name="currentWidth">Width of the current.</param>
  57. /// <param name="newHeight">The new height.</param>
  58. /// <returns>System.Double.</returns>
  59. private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight)
  60. {
  61. double scaleFactor = newHeight;
  62. scaleFactor /= currentHeight;
  63. scaleFactor *= currentWidth;
  64. return scaleFactor;
  65. }
  66. /// <summary>
  67. /// Gets the new height.
  68. /// </summary>
  69. /// <param name="currentHeight">Height of the current.</param>
  70. /// <param name="currentWidth">Width of the current.</param>
  71. /// <param name="newWidth">The new width.</param>
  72. /// <returns>System.Double.</returns>
  73. private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth)
  74. {
  75. double scaleFactor = newWidth;
  76. scaleFactor /= currentWidth;
  77. scaleFactor *= currentHeight;
  78. return scaleFactor;
  79. }
  80. }
  81. }