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;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
using Windows.Web.Http;
|
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
|
|
|
|
|
|
using System.Net.Http;
|
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
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
image = await LoadHttpImageAsync(uri);
|
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-06-13 21:38:01 +02:00
|
|
|
|
private static async Task<ImageSource> LoadHttpImageAsync(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-15 23:29:03 +02:00
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2018-08-15 23:29:03 +02:00
|
|
|
|
if (!response.IsSuccessStatusCode)
|
2018-08-08 23:31:52 +02:00
|
|
|
|
{
|
2018-08-15 23:29:03 +02:00
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
|
|
|
|
}
|
2019-06-13 21:38:01 +02:00
|
|
|
|
else if (ImageAvailable(response.Headers))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
image = await LoadImageAsync(response.Content);
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
2018-08-09 18:58:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
return image;
|
2018-08-08 23:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|