XAML-Map-Control/MapControl/WinUI/ImageLoader.WinUI.cs

121 lines
4.1 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.IO;
2022-11-30 17:59:38 +01:00
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
2022-11-30 17:59:38 +01:00
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
2021-06-14 21:41:37 +02:00
#if WINUI
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
#else
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
2021-06-14 21:41:37 +02:00
#endif
namespace MapControl
{
public static partial class ImageLoader
{
public static ImageSource LoadImage(Uri uri)
{
return new BitmapImage(uri);
}
public static async Task<WriteableBitmap> LoadImageAsync(BitmapDecoder decoder)
{
var image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
var pixelData = await decoder.GetPixelDataAsync(
2023-08-18 07:39:38 +02:00
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
pixelData.DetachPixelData().CopyTo(image.PixelBuffer);
return image;
}
2023-11-23 16:06:25 +01:00
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
{
// WinUI BitmapImage produces visual artifacts with Bing Maps Aerial (or all JPEG?)
// images in a tile raster, where thin white lines may appear as gaps between tiles.
// Alternatives are SoftwareBitmapSource or WriteableBitmap.
#if false
2023-08-19 22:07:27 +02:00
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
#else
return await LoadImageAsync(await BitmapDecoder.CreateAsync(stream));
2023-08-19 22:07:27 +02:00
#endif
}
2021-07-02 11:35:20 +02:00
public static Task<ImageSource> LoadImageAsync(Stream stream)
{
2021-07-02 11:35:20 +02:00
return LoadImageAsync(stream.AsRandomAccessStream());
}
public static async Task<ImageSource> LoadImageAsync(string path)
{
ImageSource image = null;
2022-12-06 18:07:38 +01:00
path = FilePath.GetFullPath(path);
2022-12-05 23:34:03 +01:00
if (File.Exists(path))
{
2022-12-05 23:34:03 +01:00
var file = await StorageFile.GetFileFromPathAsync(path);
using (var stream = await file.OpenReadAsync())
{
image = await LoadImageAsync(stream);
}
}
return image;
}
2022-11-30 17:59:38 +01:00
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
2022-11-30 17:59:38 +01:00
{
WriteableBitmap image = null;
IProgress<double> progress1 = null;
IProgress<double> progress2 = null;
if (progress != null)
{
var p1 = 0d;
var p2 = 0d;
progress1 = new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); });
progress2 = new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); });
}
var images = await Task.WhenAll(LoadImageAsync(uri1, progress1), LoadImageAsync(uri2, progress2));
2022-11-30 17:59:38 +01:00
2023-08-18 07:03:52 +02:00
if (images.Length == 2 &&
images[0] is WriteableBitmap image1 &&
images[1] is WriteableBitmap image2 &&
image1.PixelHeight == image2.PixelHeight)
2022-11-30 17:59:38 +01:00
{
2023-08-18 09:07:18 +02:00
var buffer1 = image1.PixelBuffer;
var buffer2 = image2.PixelBuffer;
var stride1 = (uint)image1.PixelWidth * 4;
var stride2 = (uint)image2.PixelWidth * 4;
var stride = stride1 + stride2;
var height = image1.PixelHeight;
2022-11-30 17:59:38 +01:00
2023-08-18 09:07:18 +02:00
image = new WriteableBitmap(image1.PixelWidth + image2.PixelWidth, height);
2022-11-30 17:59:38 +01:00
2023-08-18 09:07:18 +02:00
var buffer = image.PixelBuffer;
for (uint y = 0; y < height; y++)
2022-11-30 17:59:38 +01:00
{
2023-08-18 09:07:18 +02:00
buffer1.CopyTo(y * stride1, buffer, y * stride, stride1);
buffer2.CopyTo(y * stride2, buffer, y * stride + stride1, stride2);
2022-11-30 17:59:38 +01:00
}
}
return image;
}
}
}