public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var timer = Stopwatch.StartNew();

            var userProfilePhoto = value as TLUserProfilePhoto;

            if (userProfilePhoto != null)
            {
                var location = userProfilePhoto.PhotoBig as TLFileLocation;
                if (location != null)
                {
                    return(DefaultPhotoConverter.ReturnOrEnqueueImage(timer, location, userProfilePhoto, new TLInt(0)));
                }
            }

            var chatPhoto = value as TLChatPhoto;

            if (chatPhoto != null)
            {
                var location = chatPhoto.PhotoBig as TLFileLocation;
                if (location != null)
                {
                    return(DefaultPhotoConverter.ReturnOrEnqueueImage(timer, location, chatPhoto, new TLInt(0)));
                }
            }

            return(null);
        }
        private static BitmapImage ReturnImageBySize(TLPhoto photo, double width)
        {
            TLPhotoSize size  = null;
            var         sizes = photo.Sizes.OfType <TLPhotoSize>();

            foreach (var photoSize in sizes)
            {
                if (size == null ||
                    Math.Abs(width - size.W.Value) > Math.Abs(width - photoSize.W.Value))
                {
                    size = photoSize;
                }
            }

            if (size != null)
            {
                var location = size.Location as TLFileLocation;
                if (location != null)
                {
                    var timer = Stopwatch.StartNew();
                    var image = DefaultPhotoConverter.ReturnImage(timer, location);
                    if (image != null)
                    {
                        return(image);
                    }
                }
            }
            return(null);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var timer = Stopwatch.StartNew();

            var photoSize = value as TLPhotoSize;

            if (photoSize != null)
            {
                var location = photoSize.Location as TLFileLocation;
                if (location != null)
                {
                    return(DefaultPhotoConverter.ReturnImage(timer, location));
                }
            }

            return(null);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var bagroundItem = value as BackgroundItem;

            if (bagroundItem == null)
            {
                return(null);
            }

            if (string.Equals(bagroundItem.Name, Constants.LibraryBackgroundString))
            {
                var fileName = bagroundItem.IsoFileName;
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.FileExists(fileName))
                        {
                            return(null);
                        }

                        BitmapImage imageSource;

                        try
                        {
                            using (var stream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                            {
                                stream.Seek(0, SeekOrigin.Begin);
                                var b = new BitmapImage();
                                b.CreateOptions = CreateOptions;
                                b.SetSource(stream);
                                imageSource = b;
                            }
                        }
                        catch (Exception)
                        {
                            return(null);
                        }

                        return(imageSource);
                    }
                }

                return(bagroundItem.SourceString);
            }

            if (bagroundItem.Name.StartsWith("telegram", StringComparison.OrdinalIgnoreCase))
            {
                var fileName = bagroundItem.IsoFileName;
                if (!string.IsNullOrEmpty(fileName))
                {
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (store.FileExists(fileName))
                        {
                            BitmapImage imageSource;

                            try
                            {
                                using (var stream = store.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                                {
                                    stream.Seek(0, SeekOrigin.Begin);
                                    var b = new BitmapImage();
                                    b.CreateOptions = CreateOptions;
                                    b.SetSource(stream);
                                    imageSource = b;
                                }

                                return(imageSource);
                            }
                            catch (Exception)
                            {
                                return(null);
                            }
                        }
                    }
                }
            }

            if (bagroundItem.Wallpaper != null)
            {
                var    width = 99.0;
                double result;
                if (Double.TryParse((string)parameter, out result))
                {
                    width = result;
                }

                var size = GetPhotoSize(bagroundItem.Wallpaper.Sizes, width);

                if (size != null)
                {
                    var location = size.Location as TLFileLocation;
                    if (location != null)
                    {
                        return(DefaultPhotoConverter.ReturnOrEnqueueImage(null, false, location, bagroundItem.Wallpaper, size.Size, null));
                    }
                }
            }

            var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible;
            var image        = new BitmapImage();

            image.CreateOptions = CreateOptions;
            image.UriSource     = isLightTheme? new Uri("/Images/W10M/default.png", UriKind.Relative) : null;
            return(image);
        }