2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2019-03-27 18:39:59 +01:00
|
|
|
|
// © 2019 Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using System.Windows.Media;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2018-08-08 23:31:52 +02:00
|
|
|
|
public static partial class ImageLoader
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static ImageSource LoadImage(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
var bitmapImage = new BitmapImage();
|
|
|
|
|
|
bitmapImage.BeginInit();
|
|
|
|
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
|
bitmapImage.StreamSource = stream;
|
|
|
|
|
|
bitmapImage.EndInit();
|
|
|
|
|
|
bitmapImage.Freeze();
|
|
|
|
|
|
return bitmapImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2018-08-16 19:43:16 +02:00
|
|
|
|
return Task.Run(() => LoadImage(stream));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static ImageSource LoadImage(byte[] buffer)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new MemoryStream(buffer))
|
|
|
|
|
|
{
|
2018-08-16 19:43:16 +02:00
|
|
|
|
return LoadImage(stream);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2018-08-16 19:43:16 +02:00
|
|
|
|
return Task.Run(() => LoadImage(buffer));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private static async Task<ImageSource> LoadImageAsync(HttpContent content)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await content.CopyToAsync(stream);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2019-06-13 21:38:01 +02:00
|
|
|
|
|
2018-08-16 19:43:16 +02:00
|
|
|
|
return await LoadImageAsync(stream);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private static ImageSource LoadLocalImage(Uri uri)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
ImageSource image = null;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var stream = File.OpenRead(path))
|
|
|
|
|
|
{
|
2018-12-09 17:14:32 +01:00
|
|
|
|
image = LoadImage(stream);
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-09 17:14:32 +01:00
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private static Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
|
|
|
|
|
return Task.Run(() => LoadLocalImage(uri));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
internal class HttpStreamResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public readonly MemoryStream Stream;
|
|
|
|
|
|
public readonly TimeSpan? MaxAge;
|
|
|
|
|
|
|
|
|
|
|
|
public HttpStreamResponse(MemoryStream stream, TimeSpan? maxAge)
|
|
|
|
|
|
{
|
|
|
|
|
|
Stream = stream;
|
|
|
|
|
|
MaxAge = maxAge;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static async Task<HttpStreamResponse> LoadHttpStreamAsync(Uri uri)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
HttpStreamResponse response = null;
|
2018-08-09 18:21:16 +02:00
|
|
|
|
|
|
|
|
|
|
try
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
using (var responseMessage = await HttpClient.GetAsync(uri))
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (responseMessage.IsSuccessStatusCode)
|
2018-08-09 18:21:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
MemoryStream stream = null;
|
|
|
|
|
|
TimeSpan? maxAge = null;
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
if (ImageAvailable(responseMessage.Headers))
|
2018-08-09 18:21:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
stream = new MemoryStream();
|
2019-06-13 21:38:01 +02:00
|
|
|
|
await responseMessage.Content.CopyToAsync(stream);
|
2018-08-09 18:21:16 +02:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2019-06-13 21:38:01 +02:00
|
|
|
|
|
|
|
|
|
|
maxAge = responseMessage.Headers.CacheControl?.MaxAge;
|
2018-08-09 18:21:16 +02:00
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
response = new HttpStreamResponse(stream, maxAge);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)responseMessage.StatusCode, responseMessage.ReasonPhrase);
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-08-09 18:21:16 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
return response;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 21:38:01 +02:00
|
|
|
|
private static bool ImageAvailable(HttpResponseHeaders responseHeaders)
|
2017-10-27 17:15:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
IEnumerable<string> tileInfo;
|
|
|
|
|
|
return !responseHeaders.TryGetValues("X-VE-Tile-Info", out tileInfo) || !tileInfo.Contains("no-tile");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|