2018-08-08 23:31:52 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// <20> 2019 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;
|
|
|
|
|
|
#if WINDOWS_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>
|
|
|
|
|
|
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-08-09 18:58:47 +02:00
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", 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-13 08:49:49 +02:00
|
|
|
|
internal static async Task<HttpResponse> GetHttpResponseAsync(Uri uri, bool continueOnCapturedContext = true)
|
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
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
using (var responseMessage = await HttpClient.GetAsync(uri).ConfigureAwait(continueOnCapturedContext))
|
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-13 08:49:49 +02:00
|
|
|
|
response = await HttpResponse.Create(responseMessage, continueOnCapturedContext);
|
2019-07-13 00:08:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-07-13 08:49:49 +02:00
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", 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)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
internal class HttpResponse
|
2019-07-13 00:08:56 +02:00
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
public byte[] Buffer { get; private set; }
|
2019-07-13 08:49:49 +02:00
|
|
|
|
public TimeSpan? MaxAge { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
internal static async Task<HttpResponse> Create(HttpResponseMessage message, bool continueOnCapturedContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = new HttpResponse();
|
|
|
|
|
|
IEnumerable<string> tileInfo;
|
2019-07-13 00:08:56 +02:00
|
|
|
|
|
2019-07-13 08:49:49 +02:00
|
|
|
|
if (!message.Headers.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile"))
|
|
|
|
|
|
{
|
2019-07-13 19:53:03 +02:00
|
|
|
|
response.Buffer = await message.Content.ReadAsByteArrayAsync().ConfigureAwait(continueOnCapturedContext);
|
2019-07-13 08:49:49 +02:00
|
|
|
|
response.MaxAge = message.Headers.CacheControl?.MaxAge;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|