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.IO;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Net.Http.Headers;
|
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>
|
|
|
|
|
|
/// The HttpClient instance used when image data is downloaded from a web resource.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static HttpClient HttpClient { get; set; } = new HttpClient();
|
|
|
|
|
|
|
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 00:08:56 +02:00
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ImageAvailable(response.Headers))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await response.Content.CopyToAsync(stream);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
|
|
image = await LoadImageAsync(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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 00:08:56 +02:00
|
|
|
|
internal class ImageStream : MemoryStream
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
public TimeSpan? MaxAge { get; set; }
|
|
|
|
|
|
}
|
2018-08-08 23:31:52 +02:00
|
|
|
|
|
2019-07-13 00:08:56 +02:00
|
|
|
|
internal static async Task<ImageStream> LoadImageStreamAsync(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
ImageStream stream = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
using (var response = await HttpClient.GetAsync(uri).ConfigureAwait(false))
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2019-07-13 00:08:56 +02:00
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream = new ImageStream();
|
|
|
|
|
|
|
|
|
|
|
|
if (ImageAvailable(response.Headers))
|
|
|
|
|
|
{
|
|
|
|
|
|
await response.Content.CopyToAsync(stream).ConfigureAwait(false);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
|
|
stream.MaxAge = response.Headers.CacheControl?.MaxAge;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
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 00:08:56 +02:00
|
|
|
|
return stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool ImageAvailable(HttpResponseHeaders responseHeaders)
|
|
|
|
|
|
{
|
|
|
|
|
|
IEnumerable<string> tileInfo;
|
|
|
|
|
|
|
|
|
|
|
|
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|