// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control // © 2021 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Threading.Tasks; #if WINUI using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; #elif WINDOWS_UWP using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; #else using System.Windows.Media; using System.Windows.Media.Imaging; #endif namespace MapControl { public static partial class ImageLoader { /// /// The System.Net.Http.HttpClient instance used to download images via a http or https Uri. /// public static HttpClient HttpClient { get; set; } = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; public static async Task LoadImageAsync(Uri uri) { ImageSource image = null; try { if (!uri.IsAbsoluteUri || uri.Scheme == "file") { image = await LoadImageAsync(uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString); } else if (uri.Scheme == "http" || uri.Scheme == "https") { var response = await GetHttpResponseAsync(uri); if (response != null && response.Buffer != null) { image = await LoadImageAsync(response.Buffer); } } else { image = new BitmapImage(uri); } } catch (Exception ex) { Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}"); } return image; } internal class HttpResponse { public byte[] Buffer { get; } public TimeSpan? MaxAge { get; } public HttpResponse(byte[] buffer, TimeSpan? maxAge) { Buffer = buffer; MaxAge = maxAge; } } internal static async Task GetHttpResponseAsync(Uri uri) { HttpResponse response = null; try { using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)) { if (responseMessage.IsSuccessStatusCode) { byte[] buffer = null; if (!responseMessage.Headers.TryGetValues("X-VE-Tile-Info", out IEnumerable tileInfo) || !tileInfo.Contains("no-tile")) { buffer = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false); } response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge); } else { Debug.WriteLine($"ImageLoader: {uri}: {(int)responseMessage.StatusCode} {responseMessage.ReasonPhrase}"); } } } catch (Exception ex) { Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}"); } return response; } } }