using MediaBrowser.Model.Dto; using MediaBrowser.Model.Net; using System; using System.Globalization; using System.Windows.Data; namespace MediaBrowser.UI.Converters { /// /// Class UserImageConverter /// public class UserImageConverter : IValueConverter { /// /// Converts a value. /// /// The value produced by the binding source. /// The type of the binding target property. /// The converter parameter to use. /// The culture to use in the converter. /// A converted value. If the method returns null, the valid null value is used. public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var user = value as UserDto; if (user != null && user.HasPrimaryImage) { var config = parameter as string; int? maxWidth = null; int? maxHeight = null; int? width = null; int? height = null; if (!string.IsNullOrEmpty(config)) { var vals = config.Split(','); width = GetSize(vals[0]); height = GetSize(vals[1]); maxWidth = GetSize(vals[2]); maxHeight = GetSize(vals[3]); } var uri = App.Instance.ApiClient.GetUserImageUrl(user, new ImageOptions { Width = width, Height = height, MaxWidth = maxWidth, MaxHeight = maxHeight, Quality = 100 }); try { return App.Instance.GetRemoteBitmapAsync(uri).Result; } catch (HttpException) { } } return null; } /// /// Gets the size. /// /// The val. /// System.Nullable{System.Int32}. private int? GetSize(string val) { if (string.IsNullOrEmpty(val) || val == "0") { return null; } return int.Parse(val); } /// /// Converts a value. /// /// The value that is produced by the binding target. /// The type to convert to. /// The converter parameter to use. /// The culture to use in the converter. /// A converted value. If the method returns null, the valid null value is used. /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }