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

175 lines
5.7 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2022-01-14 20:22:56 +01:00
// © 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2022-11-30 17:59:38 +01:00
using System.Diagnostics;
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 async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
{
var image = new BitmapImage();
await image.SetSourceAsync(stream);
return image;
}
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());
}
2022-11-30 17:59:38 +01:00
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
2021-07-02 11:35:20 +02:00
{
using (var stream = new MemoryStream(buffer))
{
2022-11-30 22:18:45 +01:00
// Must await method before closing the stream.
//
return await LoadImageAsync(stream);
}
}
public static async Task<ImageSource> LoadImageAsync(string path)
{
ImageSource image = null;
2022-12-05 23:34:03 +01:00
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
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
public static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri, IProgress<double> progress = null)
{
WriteableBitmap image = null;
progress?.Report(0d);
try
{
if (!uri.IsAbsoluteUri || uri.IsFile)
{
var file = await StorageFile.GetFileFromPathAsync(uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString);
using (var stream = await file.OpenReadAsync())
{
image = await LoadWriteableBitmapAsync(stream);
}
}
else if (uri.Scheme == "http" || uri.Scheme == "https")
{
var response = await GetHttpResponseAsync(uri, progress);
if (response != null && response.Buffer != null)
{
using (var stream = new MemoryStream(response.Buffer))
{
image = await LoadWriteableBitmapAsync(stream.AsRandomAccessStream());
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"ImageLoader: {uri}: {ex.Message}");
}
progress?.Report(1d);
return image;
}
public static async Task<WriteableBitmap> LoadWriteableBitmapAsync(IRandomAccessStream stream)
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Straight,
new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
var pixels = pixelData.DetachPixelData();
var image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
using (var pixelStream = image.PixelBuffer.AsStream())
{
await pixelStream.WriteAsync(pixels, 0, pixels.Length);
}
return image;
}
internal static async Task<WriteableBitmap> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
{
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(
LoadWriteableBitmapAsync(uri1, progress1),
LoadWriteableBitmapAsync(uri2, progress2));
if (images.Length == 2 &&
images[0] != null &&
images[1] != null &&
images[0].PixelHeight == images[1].PixelHeight)
{
var width = images[0].PixelWidth + images[1].PixelWidth;
var height = images[1].PixelHeight;
var stride1 = images[0].PixelWidth * 4;
var stride2 = images[1].PixelWidth * 4;
var buffer1 = images[0].PixelBuffer.ToArray();
var buffer2 = images[1].PixelBuffer.ToArray();
image = new WriteableBitmap(width, height);
using (var pixelStream = image.PixelBuffer.AsStream())
{
for (var i = 0; i < height; i++)
{
await pixelStream.WriteAsync(buffer1, i * stride1, stride1);
await pixelStream.WriteAsync(buffer2, i * stride2, stride2);
}
}
}
return image;
}
}
}