mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
73 lines
2.1 KiB
C#
73 lines
2.1 KiB
C#
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
// © 2019 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();
|
|
|
|
public static async Task<ImageSource> LoadImageAsync(Uri uri)
|
|
{
|
|
ImageSource image = null;
|
|
|
|
try
|
|
{
|
|
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
|
{
|
|
image = await LoadImageAsync(uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString);
|
|
}
|
|
else if (uri.Scheme == "http" || uri.Scheme == "https")
|
|
{
|
|
image = await LoadHttpImageAsync(uri);
|
|
}
|
|
else
|
|
{
|
|
image = new BitmapImage(uri);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
|
}
|
|
|
|
return image;
|
|
}
|
|
|
|
private static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
|
{
|
|
ImageSource image = null;
|
|
|
|
using (var response = await HttpClient.GetAsync(uri))
|
|
{
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
|
}
|
|
else if (ImageAvailable(response.Headers))
|
|
{
|
|
image = await LoadImageAsync(response.Content);
|
|
}
|
|
}
|
|
|
|
return image;
|
|
}
|
|
}
|
|
} |