mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-02-13 03:04:13 +01:00
Version 4.10.0: Improved ImageLoader and MBTiles.
This commit is contained in:
parent
eca9178971
commit
bdcd2597a1
140
MBTiles/Shared/MBTileData.cs
Normal file
140
MBTiles/Shared/MBTileData.cs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using SQLiteCommand = Microsoft.Data.Sqlite.SqliteCommand;
|
||||
using SQLiteConnection = Microsoft.Data.Sqlite.SqliteConnection;
|
||||
#else
|
||||
using System.Data.SQLite;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class MBTileData : IDisposable
|
||||
{
|
||||
private readonly SQLiteConnection connection;
|
||||
|
||||
public MBTileData(string file)
|
||||
{
|
||||
connection = new SQLiteConnection("Data Source=" + file);
|
||||
}
|
||||
|
||||
public Task OpenAsync()
|
||||
{
|
||||
return connection.OpenAsync();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
public async Task<IDictionary<string, string>> ReadMetadataAsync()
|
||||
{
|
||||
var metadata = new Dictionary<string, string>();
|
||||
|
||||
try
|
||||
{
|
||||
using (var command = new SQLiteCommand("select * from metadata", connection))
|
||||
{
|
||||
var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
metadata[(string)reader["name"]] = (string)reader["value"];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileData: " + ex.Message);
|
||||
}
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
public async Task WriteMetadataAsync(IDictionary<string, object> metadata)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var command = new SQLiteCommand("create table if not exists metadata (name string, value string)", connection))
|
||||
{
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
foreach (var keyValue in metadata)
|
||||
{
|
||||
using (var command = new SQLiteCommand("insert or replace into metadata (name, value) values (@n, @v)", connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@n", keyValue.Key);
|
||||
command.Parameters.AddWithValue("@v", keyValue.Value.ToString());
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileData: " + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<byte[]> ReadImageBufferAsync(int x, int y, int zoomLevel)
|
||||
{
|
||||
byte[] imageBuffer = null;
|
||||
|
||||
try
|
||||
{
|
||||
using (var command = new SQLiteCommand("select tile_data from tiles where zoom_level=@z and tile_column=@x and tile_row=@y", connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@z", zoomLevel);
|
||||
command.Parameters.AddWithValue("@x", x);
|
||||
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
||||
|
||||
imageBuffer = await command.ExecuteScalarAsync() as byte[];
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileData: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
||||
}
|
||||
|
||||
return imageBuffer;
|
||||
}
|
||||
|
||||
public async Task WriteImageBufferAsync(int x, int y, int zoomLevel, byte[] imageBuffer)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var command = new SQLiteCommand("create table if not exists tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob)", connection))
|
||||
{
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
using (var command = new SQLiteCommand("insert or replace into tiles (zoom_level, tile_column, tile_row, tile_data) values (@z, @x, @y, @b)", connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@z", zoomLevel);
|
||||
command.Parameters.AddWithValue("@x", x);
|
||||
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
||||
command.Parameters.AddWithValue("@b", imageBuffer);
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileData: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
#else
|
||||
|
|
@ -14,7 +15,7 @@ namespace MapControl
|
|||
{
|
||||
public static readonly DependencyProperty FileProperty = DependencyProperty.Register(
|
||||
nameof(File), typeof(string), typeof(MBTileLayer),
|
||||
new PropertyMetadata(null, (o, e) => ((MBTileLayer)o).FilePropertyChanged((string)e.NewValue)));
|
||||
new PropertyMetadata(null, async (o, e) => await ((MBTileLayer)o).FilePropertyChanged((string)e.NewValue)));
|
||||
|
||||
public MBTileLayer()
|
||||
: this(new TileImageLoader())
|
||||
|
|
@ -32,67 +33,67 @@ namespace MapControl
|
|||
set { SetValue(FileProperty, value); }
|
||||
}
|
||||
|
||||
private void FilePropertyChanged(string file)
|
||||
private async Task FilePropertyChanged(string file)
|
||||
{
|
||||
MBTileSource ts;
|
||||
var mbTileSource = TileSource as MBTileSource;
|
||||
|
||||
if (mbTileSource != null)
|
||||
{
|
||||
if (file == null)
|
||||
{
|
||||
ClearValue(TileSourceProperty);
|
||||
|
||||
if (mbTileSource.Name != null)
|
||||
{
|
||||
ClearValue(SourceNameProperty);
|
||||
}
|
||||
|
||||
if (mbTileSource.Description != null)
|
||||
{
|
||||
ClearValue(DescriptionProperty);
|
||||
}
|
||||
|
||||
if (mbTileSource.MinZoom.HasValue)
|
||||
{
|
||||
ClearValue(MinZoomLevelProperty);
|
||||
}
|
||||
|
||||
if (mbTileSource.MaxZoom.HasValue)
|
||||
{
|
||||
ClearValue(MaxZoomLevelProperty);
|
||||
}
|
||||
}
|
||||
|
||||
mbTileSource.Dispose();
|
||||
}
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
ts = new MBTileSource(file);
|
||||
mbTileSource = new MBTileSource(file);
|
||||
|
||||
if (ts.Metadata.ContainsKey("name"))
|
||||
await mbTileSource.Initialize();
|
||||
|
||||
if (mbTileSource.Name != null)
|
||||
{
|
||||
SourceName = ts.Metadata["name"];
|
||||
SourceName = mbTileSource.Name;
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("description"))
|
||||
if (mbTileSource.Description != null)
|
||||
{
|
||||
Description = ts.Metadata["description"];
|
||||
Description = mbTileSource.Description;
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("minzoom"))
|
||||
if (mbTileSource.MinZoom.HasValue)
|
||||
{
|
||||
int minZoom;
|
||||
if (int.TryParse(ts.Metadata["minzoom"], out minZoom))
|
||||
{
|
||||
MinZoomLevel = minZoom;
|
||||
}
|
||||
MinZoomLevel = mbTileSource.MinZoom.Value;
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("maxzoom"))
|
||||
if (mbTileSource.MaxZoom.HasValue)
|
||||
{
|
||||
int maxZoom;
|
||||
if (int.TryParse(ts.Metadata["maxzoom"], out maxZoom))
|
||||
{
|
||||
MaxZoomLevel = maxZoom;
|
||||
}
|
||||
MaxZoomLevel = mbTileSource.MaxZoom.Value;
|
||||
}
|
||||
|
||||
TileSource = ts;
|
||||
}
|
||||
else if ((ts = TileSource as MBTileSource) != null)
|
||||
{
|
||||
ClearValue(TileSourceProperty);
|
||||
|
||||
if (ts.Metadata.ContainsKey("name"))
|
||||
{
|
||||
ClearValue(SourceNameProperty);
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("description"))
|
||||
{
|
||||
ClearValue(DescriptionProperty);
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("minzoom"))
|
||||
{
|
||||
ClearValue(MinZoomLevelProperty);
|
||||
}
|
||||
|
||||
if (ts.Metadata.ContainsKey("maxzoom"))
|
||||
{
|
||||
ClearValue(MaxZoomLevelProperty);
|
||||
}
|
||||
TileSource = mbTileSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
75
MBTiles/Shared/MBTileSource.cs
Normal file
75
MBTiles/Shared/MBTileSource.cs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml.Media;
|
||||
#else
|
||||
using System.Windows.Media;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class MBTileSource : TileSource, IDisposable
|
||||
{
|
||||
private readonly MBTileData tileData;
|
||||
|
||||
public string Name { get; private set; }
|
||||
public string Description { get; private set; }
|
||||
public int? MinZoom { get; private set; }
|
||||
public int? MaxZoom { get; private set; }
|
||||
|
||||
public MBTileSource(string file)
|
||||
{
|
||||
tileData = new MBTileData(file);
|
||||
}
|
||||
|
||||
public async Task Initialize()
|
||||
{
|
||||
await tileData.OpenAsync();
|
||||
|
||||
var metadata = await tileData.ReadMetadataAsync();
|
||||
|
||||
string name;
|
||||
string description;
|
||||
string minzoom;
|
||||
string maxzoom;
|
||||
int minZoom;
|
||||
int maxZoom;
|
||||
|
||||
if (metadata.TryGetValue("name", out name))
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
if (metadata.TryGetValue("description", out description))
|
||||
{
|
||||
Description = description;
|
||||
}
|
||||
|
||||
if (metadata.TryGetValue("minzoom", out minzoom) && int.TryParse(minzoom, out minZoom))
|
||||
{
|
||||
MinZoom = minZoom;
|
||||
}
|
||||
|
||||
if (metadata.TryGetValue("maxzoom", out maxzoom) && int.TryParse(maxzoom, out maxZoom))
|
||||
{
|
||||
MaxZoom = maxZoom;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
tileData.Dispose();
|
||||
}
|
||||
|
||||
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
||||
{
|
||||
var buffer = await tileData.ReadImageBufferAsync(x, y, zoomLevel);
|
||||
|
||||
return buffer != null ? await ImageLoader.CreateImageSourceAsync(buffer) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,85 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Windows.Storage.Streams;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class MBTileSource : TileSource, IDisposable
|
||||
{
|
||||
private readonly SqliteConnection connection;
|
||||
|
||||
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
||||
|
||||
public MBTileSource(string file)
|
||||
{
|
||||
connection = new SqliteConnection("Data Source=" + file);
|
||||
connection.Open();
|
||||
|
||||
using (var command = new SqliteCommand("select * from metadata", connection))
|
||||
{
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
Metadata[(string)reader["name"]] = (string)reader["value"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
|
||||
try
|
||||
{
|
||||
using (var command = new SqliteCommand("select tile_data from tiles where zoom_level=@z and tile_column=@x and tile_row=@y", connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@z", zoomLevel);
|
||||
command.Parameters.AddWithValue("@x", x);
|
||||
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
||||
|
||||
var buffer = await command.ExecuteScalarAsync() as byte[];
|
||||
|
||||
if (buffer != null)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(buffer.AsBuffer());
|
||||
stream.Seek(0);
|
||||
|
||||
imageSource = await ImageLoader.CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileSource: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
||||
}
|
||||
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && connection != null)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -40,10 +40,15 @@
|
|||
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Shared\MBTileData.cs">
|
||||
<Link>MBTileData.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\MBTileLayer.cs">
|
||||
<Link>MBTileLayer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="MBTileSource.UWP.cs" />
|
||||
<Compile Include="..\Shared\MBTileSource.cs">
|
||||
<Link>MBTileSource.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<EmbeddedResource Include="Properties\MBTiles.UWP.rd.xml" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SQLite;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public class MBTileSource : TileSource, IDisposable
|
||||
{
|
||||
private readonly SQLiteConnection connection;
|
||||
|
||||
public IDictionary<string, string> Metadata { get; } = new Dictionary<string, string>();
|
||||
|
||||
public MBTileSource(string file)
|
||||
{
|
||||
connection = new SQLiteConnection("Data Source=" + file + ";Version=3;");
|
||||
connection.Open();
|
||||
|
||||
using (var command = new SQLiteCommand("select * from metadata", connection))
|
||||
{
|
||||
var reader = command.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
Metadata[(string)reader["name"]] = (string)reader["value"];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<ImageSource> LoadImageAsync(int x, int y, int zoomLevel)
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
|
||||
try
|
||||
{
|
||||
using (var command = new SQLiteCommand("select tile_data from tiles where zoom_level=@z and tile_column=@x and tile_row=@y", connection))
|
||||
{
|
||||
command.Parameters.AddWithValue("@z", zoomLevel);
|
||||
command.Parameters.AddWithValue("@x", x);
|
||||
command.Parameters.AddWithValue("@y", (1 << zoomLevel) - y - 1);
|
||||
|
||||
var buffer = await command.ExecuteScalarAsync() as byte[];
|
||||
|
||||
if (buffer != null)
|
||||
{
|
||||
using (var stream = new MemoryStream(buffer))
|
||||
{
|
||||
imageSource = await ImageLoader.CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("MBTileSource: {0}/{1}/{2}: {3}", zoomLevel, x, y, ex.Message);
|
||||
}
|
||||
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && connection != null)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,10 +45,15 @@
|
|||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Shared\MBTileData.cs">
|
||||
<Link>MBTileData.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Shared\MBTileLayer.cs">
|
||||
<Link>MBTileLayer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="MBTileSource.WPF.cs" />
|
||||
<Compile Include="..\Shared\MBTileSource.cs">
|
||||
<Link>MBTileSource.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -51,27 +51,20 @@ namespace MapControl
|
|||
return imageSource;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
private static async Task<ImageSource> LoadHttpImageAsync(Uri uri)
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
|
||||
try
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
{
|
||||
using (var response = await HttpClient.GetAsync(uri))
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
{
|
||||
imageSource = await CreateImageSourceAsync(response.Content);
|
||||
}
|
||||
Debug.WriteLine("ImageLoader: {0}: {1} {2}", uri, (int)response.StatusCode, response.ReasonPhrase);
|
||||
}
|
||||
else if (IsTileAvailable(response.Headers))
|
||||
{
|
||||
imageSource = await CreateImageSourceAsync(response.Content);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
|
||||
return imageSource;
|
||||
|
|
|
|||
|
|
@ -4,9 +4,13 @@
|
|||
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media.Animation;
|
||||
#else
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media.Animation;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
|
|
@ -37,5 +41,11 @@ namespace MapControl
|
|||
return ((X % numTiles) + numTiles) % numTiles;
|
||||
}
|
||||
}
|
||||
|
||||
private void FadeIn()
|
||||
{
|
||||
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
||||
Image.Opacity = 1d;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Streams;
|
||||
|
|
@ -17,7 +18,52 @@ namespace MapControl
|
|||
{
|
||||
public static partial class ImageLoader
|
||||
{
|
||||
public static async Task<Tuple<IBuffer, TimeSpan?>> LoadHttpBufferAsync(Uri uri)
|
||||
public static async Task<ImageSource> CreateImageSourceAsync(IRandomAccessStream stream)
|
||||
{
|
||||
var bitmapImage = new BitmapImage();
|
||||
await bitmapImage.SetSourceAsync(stream);
|
||||
return bitmapImage;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> CreateImageSourceAsync(byte[] buffer)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await stream.WriteAsync(buffer.AsBuffer());
|
||||
stream.Seek(0);
|
||||
return await CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<ImageSource> CreateImageSourceAsync(IHttpContent content)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await content.WriteToStreamAsync(stream);
|
||||
stream.Seek(0);
|
||||
return await CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var file = await StorageFile.GetFileFromPathAsync(path);
|
||||
|
||||
using (var stream = await file.OpenReadAsync())
|
||||
{
|
||||
imageSource = await CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
internal static async Task<Tuple<IBuffer, TimeSpan?>> LoadHttpBufferAsync(Uri uri)
|
||||
{
|
||||
Tuple<IBuffer, TimeSpan?> result = null;
|
||||
|
||||
|
|
@ -52,49 +98,6 @@ namespace MapControl
|
|||
return result;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> LoadLocalImageAsync(Uri uri)
|
||||
{
|
||||
ImageSource imageSource = null;
|
||||
|
||||
try
|
||||
{
|
||||
var path = uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString;
|
||||
|
||||
if (File.Exists(path))
|
||||
{
|
||||
var file = await StorageFile.GetFileFromPathAsync(path);
|
||||
|
||||
using (var stream = await file.OpenReadAsync())
|
||||
{
|
||||
imageSource = await CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine("ImageLoader: {0}: {1}", uri, ex.Message);
|
||||
}
|
||||
|
||||
return imageSource;
|
||||
}
|
||||
|
||||
public static async Task<ImageSource> CreateImageSourceAsync(IRandomAccessStream stream)
|
||||
{
|
||||
var bitmapImage = new BitmapImage();
|
||||
await bitmapImage.SetSourceAsync(stream);
|
||||
return bitmapImage;
|
||||
}
|
||||
|
||||
private static async Task<ImageSource> CreateImageSourceAsync(IHttpContent content)
|
||||
{
|
||||
using (var stream = new InMemoryRandomAccessStream())
|
||||
{
|
||||
await content.WriteToStreamAsync(stream);
|
||||
stream.Seek(0);
|
||||
return await CreateImageSourceAsync(stream);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsTileAvailable(HttpResponseHeaderCollection responseHeaders)
|
||||
{
|
||||
return !responseHeaders.TryGetValue("X-VE-Tile-Info", out string tileInfo) || tileInfo != "no-tile";
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ using System;
|
|||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Animation;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
|
||||
namespace MapControl
|
||||
|
|
@ -28,8 +27,7 @@ namespace MapControl
|
|||
}
|
||||
else
|
||||
{
|
||||
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
||||
Image.Opacity = 1d;
|
||||
FadeIn();
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -47,8 +45,7 @@ namespace MapControl
|
|||
bitmapImage.ImageOpened -= BitmapImageOpened;
|
||||
bitmapImage.ImageFailed -= BitmapImageFailed;
|
||||
|
||||
Image.BeginAnimation(UIElement.OpacityProperty, new DoubleAnimation { From = 0d, To = 1d, Duration = FadeDuration, FillBehavior = FillBehavior.Stop });
|
||||
Image.Opacity = 1d;
|
||||
FadeIn();
|
||||
}
|
||||
|
||||
private void BitmapImageFailed(object sender, ExceptionRoutedEventArgs e)
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue