2021-01-13 21:19:27 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2022-01-14 20:22:56 +01:00
|
|
|
|
// © 2022 Clemens Fischer
|
2018-08-08 23:31:52 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Collections.Generic;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using System.Diagnostics;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.Http;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2021-06-14 21:41:37 +02:00
|
|
|
|
#if WINUI
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media;
|
|
|
|
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
2021-11-17 23:17:11 +01:00
|
|
|
|
#elif UWP
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using Windows.UI.Xaml.Media;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
#else
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using System.Windows.Media;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static partial class ImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2019-07-13 19:53:03 +02:00
|
|
|
|
/// The System.Net.Http.HttpClient instance used to download images via a http or https Uri.
|
2018-08-08 23:31:52 +02:00
|
|
|
|
/// </summary>
|
2020-11-03 16:02:31 +01:00
|
|
|
|
public static HttpClient HttpClient { get; set; } = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
|
2018-08-08 23:31:52 +02:00
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(Uri uri)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
ImageSource image = null;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
|
2018-08-09 18:58:47 +02:00
|
|
|
|
try
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2018-08-09 18:58:47 +02:00
|
|
|
|
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
|
|
|
|
|
{
|
2019-06-15 01:39:07 +02:00
|
|
|
|
image = await LoadImageAsync(uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString);
|
2018-08-09 18:58:47 +02:00
|
|
|
|
}
|
2018-08-14 23:23:43 +02:00
|
|
|
|
else if (uri.Scheme == "http" || uri.Scheme == "https")
|
2018-08-09 18:58:47 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
var response = await GetHttpResponseAsync(uri);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-13 19:53:03 +02:00
|
|
|
|
if (response != null && response.Buffer != null)
|
2019-07-13 08:49:49 +02:00
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
image = await LoadImageAsync(response.Buffer);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
image = new BitmapImage(uri);
|
2018-08-09 18:58:47 +02:00
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
catch (Exception ex)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2021-07-06 18:49:48 +02:00
|
|
|
|
Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}");
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
return image;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-22 19:47:57 +02:00
|
|
|
|
internal class HttpResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public byte[] Buffer { get; }
|
|
|
|
|
|
public TimeSpan? MaxAge { get; }
|
|
|
|
|
|
|
|
|
|
|
|
public HttpResponse(byte[] buffer, TimeSpan? maxAge)
|
|
|
|
|
|
{
|
|
|
|
|
|
Buffer = buffer;
|
|
|
|
|
|
MaxAge = maxAge;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-25 15:02:41 +02:00
|
|
|
|
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
HttpResponse response = null;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2020-09-25 15:02:41 +02:00
|
|
|
|
using (var responseMessage = await HttpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
if (responseMessage.IsSuccessStatusCode)
|
2019-07-13 00:08:56 +02:00
|
|
|
|
{
|
2019-07-22 19:47:57 +02:00
|
|
|
|
byte[] buffer = null;
|
2019-07-22 01:06:59 +02:00
|
|
|
|
|
2020-04-16 19:24:38 +02:00
|
|
|
|
if (!responseMessage.Headers.TryGetValues("X-VE-Tile-Info", out IEnumerable<string> tileInfo) ||
|
2019-07-22 19:47:57 +02:00
|
|
|
|
!tileInfo.Contains("no-tile"))
|
2019-07-22 01:06:59 +02:00
|
|
|
|
{
|
2020-09-25 15:02:41 +02:00
|
|
|
|
buffer = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
|
2019-07-22 01:06:59 +02:00
|
|
|
|
}
|
2019-07-22 19:47:57 +02:00
|
|
|
|
|
|
|
|
|
|
response = new HttpResponse(buffer, responseMessage.Headers.CacheControl?.MaxAge);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-07-06 18:49:48 +02:00
|
|
|
|
Debug.WriteLine($"ImageLoader: {uri}: {(int)responseMessage.StatusCode} {responseMessage.ReasonPhrase}");
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
}
|
2019-07-13 00:08:56 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2021-07-06 18:49:48 +02:00
|
|
|
|
Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}");
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
return response;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|