2018-08-08 23:31:52 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// <20> 2018 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Web.Http;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(Uri uri)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
|
|
|
|
|
ImageSource imageSource = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSource = await LoadLocalImageAsync(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (uri.Scheme == "http")
|
|
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
imageSource = await LoadHttpImageAsync(uri);
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
imageSource = new BitmapImage(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageSource;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-09 18:21:16 +02:00
|
|
|
|
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
|
|
|
|
|
ImageSource imageSource = null;
|
|
|
|
|
|
|
|
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
2018-08-09 18:21:16 +02:00
|
|
|
|
else if (IsTileAvailable(response.Headers))
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2018-08-09 18:21:16 +02:00
|
|
|
|
imageSource = await CreateImageSourceAsync(response.Content);
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return imageSource;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|