Version 4.10.0: Improved ImageLoader and MBTiles.

This commit is contained in:
ClemensF 2018-08-15 23:29:03 +02:00
parent eca9178971
commit bdcd2597a1
13 changed files with 403 additions and 339 deletions

View file

@ -17,7 +17,67 @@ namespace MapControl
{
public static partial class ImageLoader
{
public static async Task<Tuple<MemoryStream, TimeSpan?>> LoadHttpStreamAsync(Uri uri)
public static ImageSource CreateImageSource(Stream stream)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
public static Task<ImageSource> CreateImageSourceAsync(Stream stream)
{
return Task.Run(() => CreateImageSource(stream));
}
public static ImageSource CreateImageSource(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
return CreateImageSource(stream);
}
}
public static Task<ImageSource> CreateImageSourceAsync(byte[] buffer)
{
return Task.Run(() => CreateImageSource(buffer));
}
private static async Task<ImageSource> CreateImageSourceAsync(HttpContent content)
{
using (var stream = new MemoryStream())
{
await content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
return await CreateImageSourceAsync(stream);
}
}
private static ImageSource LoadLocalImage(Uri uri)
{
ImageSource imageSource = null;
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
if (File.Exists(path))
{
using (var stream = File.OpenRead(path))
{
imageSource = CreateImageSource(stream);
}
}
return imageSource;
}
private static Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
return Task.Run(() => LoadLocalImage(uri));
}
internal static async Task<Tuple<MemoryStream, TimeSpan?>> LoadHttpStreamAsync(Uri uri)
{
Tuple<MemoryStream, TimeSpan?> result = null;
@ -54,61 +114,6 @@ namespace MapControl
return result;
}
public static ImageSource LoadLocalImage(Uri uri)
{
ImageSource imageSource = null;
try
{
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
if (File.Exists(path))
{
using (var stream = File.OpenRead(path))
{
imageSource = CreateImageSource(stream);
}
}
}
catch (Exception ex)
{
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
}
return imageSource;
}
public static Task<ImageSource> LoadLocalImageAsync(Uri uri)
{
return Task.Run(() => LoadLocalImage(uri));
}
public static ImageSource CreateImageSource(Stream stream)
{
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
public static Task<ImageSource> CreateImageSourceAsync(Stream stream)
{
return Task.Run(() => CreateImageSource(stream));
}
private static async Task<ImageSource> CreateImageSourceAsync(HttpContent content)
{
using (var stream = new MemoryStream())
{
await content.CopyToAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
return await CreateImageSourceAsync(stream);
}
}
private static bool IsTileAvailable(HttpResponseHeaders responseHeaders)
{
IEnumerable<string> tileInfo;

View file

@ -3,10 +3,8 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace MapControl
@ -28,8 +26,7 @@ namespace MapControl
}
else
{
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0d, 1d, FadeDuration, FillBehavior.Stop));
Image.Opacity = 1d;
FadeIn();
}
}
else
@ -47,8 +44,7 @@ namespace MapControl
bitmapSource.DownloadCompleted -= BitmapDownloadCompleted;
bitmapSource.DownloadFailed -= BitmapDownloadFailed;
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation(0d, 1d, FadeDuration, FillBehavior.Stop));
Image.Opacity = 1d;
FadeIn();
}
private void BitmapDownloadFailed(object sender, ExceptionEventArgs e)