2017-10-27 17:15:18 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2025-01-01 18:57:55 +01:00
|
|
|
|
// Copyright © Clemens Fischer
|
2017-10-27 17:15:18 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2022-11-30 17:59:38 +01:00
|
|
|
|
using System;
|
2017-10-27 17:15:18 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2023-08-18 17:44:33 +02:00
|
|
|
|
using System.Windows;
|
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
|
|
|
|
{
|
2024-05-19 17:24:18 +02:00
|
|
|
|
public static ImageSource LoadImage(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BitmapImage(uri);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-18 17:44:33 +02:00
|
|
|
|
public static ImageSource LoadImage(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2023-08-18 17:44:33 +02:00
|
|
|
|
var image = new BitmapImage();
|
|
|
|
|
|
|
|
|
|
|
|
image.BeginInit();
|
|
|
|
|
|
image.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
|
image.StreamSource = stream;
|
|
|
|
|
|
image.EndInit();
|
|
|
|
|
|
image.Freeze();
|
|
|
|
|
|
|
|
|
|
|
|
return image;
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-18 17:44:33 +02:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(Stream stream)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2023-08-18 17:44:33 +02:00
|
|
|
|
return Task.FromResult(LoadImage(stream));
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-28 23:50:27 +01:00
|
|
|
|
public static Task<ImageSource> LoadImageAsync(string path)
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2024-05-22 16:15:29 +02:00
|
|
|
|
if (!File.Exists(path))
|
2018-08-15 23:29:03 +02:00
|
|
|
|
{
|
2024-05-22 16:15:29 +02:00
|
|
|
|
return Task.FromResult<ImageSource>(null);
|
|
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
|
2025-02-23 18:38:34 +01:00
|
|
|
|
using (var stream = File.OpenRead(path))
|
2024-05-22 16:15:29 +02:00
|
|
|
|
{
|
2025-02-23 18:38:34 +01:00
|
|
|
|
return LoadImageAsync(stream);
|
|
|
|
|
|
}
|
2018-08-15 23:29:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-30 17:59:38 +01:00
|
|
|
|
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
|
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
WriteableBitmap mergedBitmap = null;
|
2025-01-02 10:48:51 +01:00
|
|
|
|
var p1 = 0d;
|
|
|
|
|
|
var p2 = 0d;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-02 10:48:51 +01:00
|
|
|
|
var images = await Task.WhenAll(
|
|
|
|
|
|
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
|
|
|
|
|
|
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
|
|
|
|
|
if (images.Length == 2 &&
|
2025-01-01 19:45:40 +01:00
|
|
|
|
images[0] is BitmapSource bitmap1 &&
|
|
|
|
|
|
images[1] is BitmapSource bitmap2 &&
|
|
|
|
|
|
bitmap1.PixelHeight == bitmap2.PixelHeight &&
|
|
|
|
|
|
bitmap1.Format == bitmap2.Format &&
|
|
|
|
|
|
bitmap1.Format.BitsPerPixel % 8 == 0)
|
2022-11-30 17:59:38 +01:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
var format = bitmap1.Format;
|
|
|
|
|
|
var height = bitmap1.PixelHeight;
|
|
|
|
|
|
var width1 = bitmap1.PixelWidth;
|
|
|
|
|
|
var width2 = bitmap2.PixelWidth;
|
2023-08-18 17:44:33 +02:00
|
|
|
|
var stride1 = width1 * format.BitsPerPixel / 8;
|
|
|
|
|
|
var stride2 = width2 * format.BitsPerPixel / 8;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
var buffer1 = new byte[stride1 * height];
|
|
|
|
|
|
var buffer2 = new byte[stride2 * height];
|
|
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
bitmap1.CopyPixels(buffer1, stride1, 0);
|
|
|
|
|
|
bitmap2.CopyPixels(buffer2, stride2, 0);
|
2022-11-30 17:59:38 +01:00
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
mergedBitmap = new WriteableBitmap(width1 + width2, height, 96, 96, format, null);
|
|
|
|
|
|
mergedBitmap.WritePixels(new Int32Rect(0, 0, width1, height), buffer1, stride1, 0);
|
|
|
|
|
|
mergedBitmap.WritePixels(new Int32Rect(width1, 0, width2, height), buffer2, stride2, 0);
|
|
|
|
|
|
mergedBitmap.Freeze();
|
2022-11-30 17:59:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
return mergedBitmap;
|
2022-11-30 17:59:38 +01:00
|
|
|
|
}
|
2017-10-27 17:15:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|