Revert to using MemoryStream in WinUI ImageLoader

This commit is contained in:
ClemensFischer 2023-08-20 14:00:30 +02:00
parent 359e253d00
commit 7b404446d2
3 changed files with 17 additions and 29 deletions

View file

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -65,6 +66,14 @@ namespace MapControl
return image; return image;
} }
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
return await LoadImageAsync(stream);
}
}
internal class HttpResponse internal class HttpResponse
{ {
public byte[] Buffer { get; } public byte[] Buffer { get; }

View file

@ -31,14 +31,6 @@ namespace MapControl
return Task.FromResult(LoadImage(stream)); return Task.FromResult(LoadImage(stream));
} }
public static async Task<ImageSource> LoadImageAsync(byte[] buffer)
{
using (var stream = new MemoryStream(buffer))
{
return await LoadImageAsync(stream);
}
}
public static Task<ImageSource> LoadImageAsync(string path) public static Task<ImageSource> LoadImageAsync(string path)
{ {
return Task.Run(() => return Task.Run(() =>

View file

@ -21,32 +21,35 @@ namespace MapControl
{ {
public static partial class ImageLoader public static partial class ImageLoader
{ {
public static async Task<ImageSource> LoadImageAsync(BitmapDecoder decoder) #if false
public static async Task<SoftwareBitmapSource> LoadImageAsync(BitmapDecoder decoder)
{ {
#if USE_SOFTWAREBITMAP
var image = new SoftwareBitmapSource(); var image = new SoftwareBitmapSource();
var bitmap = await decoder.GetSoftwareBitmapAsync( var bitmap = await decoder.GetSoftwareBitmapAsync(
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
await image.SetBitmapAsync(bitmap); await image.SetBitmapAsync(bitmap);
return image;
}
#else #else
public static async Task<WriteableBitmap> LoadImageAsync(BitmapDecoder decoder)
{
var image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight); var image = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
var pixelData = await decoder.GetPixelDataAsync( var pixelData = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(),
ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
pixelData.DetachPixelData().CopyTo(image.PixelBuffer); pixelData.DetachPixelData().CopyTo(image.PixelBuffer);
#endif
return image; return image;
} }
#endif
public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream) public static async Task<ImageSource> LoadImageAsync(IRandomAccessStream stream)
{ {
// WinUI BitmapImage produces visual artifacts with Bing Maps Aerial (or all JPEG?) // 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. // images in a tile raster, where thin white lines may appear as gaps between tiles.
// Alternatives are SoftwareBitmapSource or WriteableBitmap. // Alternatives are SoftwareBitmapSource or WriteableBitmap.
#if USE_BITMAPIMAGE #if false
var image = new BitmapImage(); var image = new BitmapImage();
await image.SetSourceAsync(stream); await image.SetSourceAsync(stream);
return image; return image;
@ -60,22 +63,6 @@ namespace MapControl
return LoadImageAsync(stream.AsRandomAccessStream()); return LoadImageAsync(stream.AsRandomAccessStream());
} }
public static async Task<ImageSource> LoadImageAsync(IBuffer buffer)
{
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(buffer);
stream.Seek(0);
return await LoadImageAsync(stream);
}
}
public static Task<ImageSource> LoadImageAsync(byte[] buffer)
{
return LoadImageAsync(buffer.AsBuffer());
}
public static async Task<ImageSource> LoadImageAsync(string path) public static async Task<ImageSource> LoadImageAsync(string path)
{ {
ImageSource image = null; ImageSource image = null;