UserImageConverter.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Net;
  3. using System;
  4. using System.Globalization;
  5. using System.Windows.Data;
  6. namespace MediaBrowser.UI.Converters
  7. {
  8. /// <summary>
  9. /// Class UserImageConverter
  10. /// </summary>
  11. public class UserImageConverter : IValueConverter
  12. {
  13. /// <summary>
  14. /// Converts a value.
  15. /// </summary>
  16. /// <param name="value">The value produced by the binding source.</param>
  17. /// <param name="targetType">The type of the binding target property.</param>
  18. /// <param name="parameter">The converter parameter to use.</param>
  19. /// <param name="culture">The culture to use in the converter.</param>
  20. /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
  21. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  22. {
  23. var user = value as UserDto;
  24. if (user != null && user.HasPrimaryImage)
  25. {
  26. var config = parameter as string;
  27. int? maxWidth = null;
  28. int? maxHeight = null;
  29. int? width = null;
  30. int? height = null;
  31. if (!string.IsNullOrEmpty(config))
  32. {
  33. var vals = config.Split(',');
  34. width = GetSize(vals[0]);
  35. height = GetSize(vals[1]);
  36. maxWidth = GetSize(vals[2]);
  37. maxHeight = GetSize(vals[3]);
  38. }
  39. var uri = App.Instance.ApiClient.GetUserImageUrl(user, new ImageOptions
  40. {
  41. Width = width,
  42. Height = height,
  43. MaxWidth = maxWidth,
  44. MaxHeight = maxHeight,
  45. Quality = 100
  46. });
  47. try
  48. {
  49. return App.Instance.GetRemoteBitmapAsync(uri).Result;
  50. }
  51. catch (HttpException)
  52. {
  53. }
  54. }
  55. return null;
  56. }
  57. /// <summary>
  58. /// Gets the size.
  59. /// </summary>
  60. /// <param name="val">The val.</param>
  61. /// <returns>System.Nullable{System.Int32}.</returns>
  62. private int? GetSize(string val)
  63. {
  64. if (string.IsNullOrEmpty(val) || val == "0")
  65. {
  66. return null;
  67. }
  68. return int.Parse(val);
  69. }
  70. /// <summary>
  71. /// Converts a value.
  72. /// </summary>
  73. /// <param name="value">The value that is produced by the binding target.</param>
  74. /// <param name="targetType">The type to convert to.</param>
  75. /// <param name="parameter">The converter parameter to use.</param>
  76. /// <param name="culture">The culture to use in the converter.</param>
  77. /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
  78. /// <exception cref="System.NotImplementedException"></exception>
  79. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. }
  84. }