ImageSize.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Globalization;
  2. namespace MediaBrowser.Model.Drawing
  3. {
  4. /// <summary>
  5. /// Struct ImageSize
  6. /// </summary>
  7. public struct ImageSize
  8. {
  9. private double _height;
  10. private double _width;
  11. /// <summary>
  12. /// Gets or sets the height.
  13. /// </summary>
  14. /// <value>The height.</value>
  15. public double Height
  16. {
  17. get => _height;
  18. set => _height = value;
  19. }
  20. /// <summary>
  21. /// Gets or sets the width.
  22. /// </summary>
  23. /// <value>The width.</value>
  24. public double Width
  25. {
  26. get => _width;
  27. set => _width = value;
  28. }
  29. public bool Equals(ImageSize size)
  30. {
  31. return Width.Equals(size.Width) && Height.Equals(size.Height);
  32. }
  33. public override string ToString()
  34. {
  35. return string.Format("{0}-{1}", Width, Height);
  36. }
  37. public ImageSize(string value)
  38. {
  39. _width = 0;
  40. _height = 0;
  41. ParseValue(value);
  42. }
  43. public ImageSize(int width, int height)
  44. {
  45. _width = width;
  46. _height = height;
  47. }
  48. public ImageSize(double width, double height)
  49. {
  50. _width = width;
  51. _height = height;
  52. }
  53. private void ParseValue(string value)
  54. {
  55. if (!string.IsNullOrEmpty(value))
  56. {
  57. string[] parts = value.Split('-');
  58. if (parts.Length == 2)
  59. {
  60. if (double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var val))
  61. {
  62. _width = val;
  63. }
  64. if (double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out val))
  65. {
  66. _height = val;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }