UserImageConverter.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using MediaBrowser.Model.DTO;
  2. using MediaBrowser.UI.Controller;
  3. using System;
  4. using System.Globalization;
  5. using System.Net.Cache;
  6. using System.Windows.Data;
  7. using System.Windows.Media.Imaging;
  8. namespace MediaBrowser.UI.Converters
  9. {
  10. public class UserImageConverter : IValueConverter
  11. {
  12. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  13. {
  14. var user = value as DtoUser;
  15. if (user != null && user.HasImage)
  16. {
  17. var config = parameter as string;
  18. int? maxWidth = null;
  19. int? maxHeight = null;
  20. int? width = null;
  21. int? height = null;
  22. if (!string.IsNullOrEmpty(config))
  23. {
  24. var vals = config.Split(',');
  25. width = GetSize(vals[0]);
  26. height = GetSize(vals[1]);
  27. maxWidth = GetSize(vals[2]);
  28. maxHeight = GetSize(vals[3]);
  29. }
  30. var uri = UIKernel.Instance.ApiClient.GetUserImageUrl(user.Id, width, height, maxWidth, maxHeight, 100);
  31. return new BitmapImage(new Uri(uri), new RequestCachePolicy(RequestCacheLevel.Revalidate));
  32. }
  33. return null;
  34. }
  35. private int? GetSize(string val)
  36. {
  37. if (string.IsNullOrEmpty(val) || val == "0")
  38. {
  39. return null;
  40. }
  41. return int.Parse(val);
  42. }
  43. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  44. {
  45. throw new NotImplementedException();
  46. }
  47. }
  48. }