2017-06-25 23:05:48 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2017 Clemens Fischer
|
2016-02-23 20:07:30 +01:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2016-06-04 08:42:38 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates frozen BitmapSources from Stream or Uri.
|
|
|
|
|
|
/// </summary>
|
2017-06-25 23:05:48 +02:00
|
|
|
|
public static class BitmapSourceHelper
|
2016-02-23 20:07:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
public static BitmapSource FromStream(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
var bitmap = new BitmapImage();
|
|
|
|
|
|
|
|
|
|
|
|
bitmap.BeginInit();
|
|
|
|
|
|
bitmap.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
|
bitmap.StreamSource = stream;
|
|
|
|
|
|
bitmap.EndInit();
|
|
|
|
|
|
bitmap.Freeze();
|
|
|
|
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static BitmapSource FromUri(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2017-06-25 23:05:48 +02:00
|
|
|
|
using (var response = WebRequest.Create(uri).GetResponse())
|
2016-02-23 20:07:30 +01:00
|
|
|
|
using (var responseStream = response.GetResponseStream())
|
|
|
|
|
|
using (var memoryStream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
responseStream.CopyTo(memoryStream);
|
|
|
|
|
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
2017-06-25 23:05:48 +02:00
|
|
|
|
|
|
|
|
|
|
return FromStream(memoryStream);
|
2016-02-23 20:07:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.WriteLine(ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-25 23:05:48 +02:00
|
|
|
|
return null;
|
2016-02-23 20:07:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|