private async Task <BitmapImage> DownloadImageUrl(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                if (!ChatImageControl.bitmapImages.ContainsKey(url))
                {
                    BitmapImage bitmap = new BitmapImage();
                    using (WebClient client = new WebClient())
                    {
                        var bytes = await Task.Run <byte[]>(async() =>
                        {
                            try
                            {
                                return(await client.DownloadDataTaskAsync(url));
                            }
                            catch (Exception ex)
                            {
                                Logger.Log("Failed to download image: " + url);
                                throw ex;
                            }
                        });

                        bitmap = WindowsImageService.Load(bytes);
                    }
                    ChatImageControl.bitmapImages[url] = bitmap;
                }
                return(ChatImageControl.bitmapImages[url]);
            }
            return(null);
        }
Example #2
0
        public async Task <BitmapImage> SetUserAvatarUrl(string url)
        {
            BitmapImage bitmap = null;

            using (WebClient client = new WebClient())
            {
                var bytes = await Task.Run <byte[]>((Func <Task <byte[]> >)(async() => { return(await client.DownloadDataTaskAsync((string)url)); }));

                bitmap = WindowsImageService.Load(bytes);
            }
            this.ProfileAvatarImage.ImageSource = bitmap;
            return(bitmap);
        }
        private async Task <BitmapImage> DownloadImageUrl(string url)
        {
            if (!ChatImageControl.bitmapImages.ContainsKey(url))
            {
                BitmapImage bitmap = new BitmapImage();
                using (WebClient client = new WebClient())
                {
                    var bytes = await Task.Run <byte[]>(async() => { return(await client.DownloadDataTaskAsync(url)); });

                    bitmap = WindowsImageService.Load(bytes);
                }
                ChatImageControl.bitmapImages[url] = bitmap;
            }
            return(ChatImageControl.bitmapImages[url]);
        }