protected override Size MeasureOverride(Size availableSize) { var progress = this.Progress; if (progress != null) { if (!Double.IsInfinity(availableSize.Width)) { progress.Width = Math.Min(96, Math.Max(8, availableSize.Width * 0.5)); } if (!Double.IsInfinity(availableSize.Height)) { progress.Height = Math.Min(96, Math.Max(8, availableSize.Height * 0.5)); } base.MeasureOverride(availableSize); _currentSize = NormalizeSize(availableSize); return(_currentSize); } var newSize = new Size(Math.Min(Int16.MaxValue, availableSize.Width), Math.Min(Int16.MaxValue, availableSize.Height)); if (_isHttpSource && BitmapCache.GetSizeLevel(_currentSize) != BitmapCache.GetSizeLevel(newSize)) { _currentSize = newSize; RefreshSourceUri(_currentUri); } return(base.MeasureOverride(availableSize)); }
private async void SetSourceUri(Uri uri) { _currentUri = uri; if (uri.IsAbsoluteUri) { var cachedUri = uri; if (uri.Scheme == "http" || uri.Scheme == "https") { SetProgress(); _isHttpSource = true; if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled) { cachedUri = await BitmapCache.GetImageUriAsync(uri, (int)_currentSize.Width, (int)_currentSize.Height); } } if (Path.GetExtension(uri.LocalPath).Equals(".gif", StringComparison.OrdinalIgnoreCase)) { this.SetImageGif(cachedUri); } else { this.SetImage(new BitmapImage(cachedUri)); } } else { ClearImage(); ClearImageGif(); } }
private async void RefreshSourceUri(Uri uri) { if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled) { uri = await BitmapCache.GetImageUriAsync(uri, (int)_currentSize.Width, (int)_currentSize.Height); } this.SetImage(new BitmapImage(uri)); }
public static async Task <bool> DownloadImageAsync(StorageFile file, Uri uri, int maxWidth = Int32.MaxValue, int maxHeight = Int32.MaxValue) { StorageFile tempFile = null; try { using (var httpClient = new HttpClient()) { using (var httpMessage = await httpClient.GetAsync(uri)) { var cacheFolder = await BitmapCache.EnsureCacheFolderAsync(); tempFile = await cacheFolder.CreateFileAsync($"{file.Name}.tmp"); using (var fileStream = await tempFile.OpenAsync(FileAccessMode.ReadWrite)) { await httpMessage.Content.WriteToStreamAsync(fileStream); } using (var readStream = await tempFile.OpenAsync(FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(readStream); using (var writeStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateForTranscodingAsync(writeStream, decoder); await encoder.FlushAsync(); } } } } return(true); } catch { return(false); } finally { try { if (tempFile != null) { await tempFile.DeleteAsync(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("DownloadImageAsync. {0}", ex.Message); } } }
private void OnSizeChanged(object sender, SizeChangedEventArgs e) { var imageSize = _image.DesiredSize; if (imageSize.Width > 0 && imageSize.Height > 0) { if (_isHttpSource && BitmapCache.GetSizeLevel(_currentSize) != BitmapCache.GetSizeLevel(imageSize)) { _currentSize = imageSize; RefreshImage(); } } }
private async Task LoadImageAsync() { if (!_isLoadingImage && _uri != null) { _isLoadingImage = true; if (_isHttpSource) { _image.Source = await BitmapCache.LoadFromCacheAsync(_uri, (int)_currentSize.Width, (int)_currentSize.Height); } else { _image.Source = new BitmapImage(_uri); } _isLoadingImage = false; } }
private async void RefreshSourceUri(Uri uri) { try { if (!Windows.ApplicationModel.DesignMode.DesignModeEnabled) { uri = await BitmapCache.GetImageUriAsync(uri, (int)_currentSize.Width, (int)_currentSize.Height); } if (uri != null) { this.SetImage(new BitmapImage(uri)); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("RefreshSourceUri. {0}", ex.Message); } }