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>
|
2019-07-22 01:06:59 +02:00
|
|
|
|
public static HttpClient HttpClient { get; set; } = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
|
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
|
|
|
|
{
|
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-22 01:06:59 +02:00
|
|
|
|
using (var responseMessage = await HttpClient
|
|
|
|
|
|
.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead)
|
|
|
|
|
|
.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-22 01:06:59 +02:00
|
|
|
|
IEnumerable<string> tileInfo;
|
|
|
|
|
|
|
|
|
|
|
|
if (responseMessage.Headers.TryGetValues("X-VE-Tile-Info", out tileInfo) &&
|
|
|
|
|
|
tileInfo.Contains("no-tile"))
|
|
|
|
|
|
{
|
|
|
|
|
|
response = new HttpResponse(null, null); // no tile image
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
response = new HttpResponse(
|
|
|
|
|
|
await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(continueOnCapturedContext),
|
|
|
|
|
|
responseMessage.Headers.CacheControl?.MaxAge);
|
|
|
|
|
|
}
|
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-22 01:06:59 +02:00
|
|
|
|
public byte[] Buffer { get; }
|
|
|
|
|
|
public TimeSpan? MaxAge { get; }
|
2019-07-13 08:49:49 +02:00
|
|
|
|
|
2019-07-22 01:06:59 +02:00
|
|
|
|
public HttpResponse(byte[] buffer, TimeSpan? maxAge)
|
2019-07-13 08:49:49 +02:00
|
|
|
|
{
|
2019-07-22 01:06:59 +02:00
|
|
|
|
Buffer = buffer;
|
|
|
|
|
|
MaxAge = maxAge;
|
2019-07-13 08:49:49 +02:00
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|