From 21692f03a75e7762ff9ab16d878f813ca6ee2963 Mon Sep 17 00:00:00 2001 From: Empiree Date: Mon, 12 Oct 2026 19:48:13 +0200 Subject: [PATCH 1/2] [Windows] Fix Image AspectFit when the bitmap is already decoded --- .../Handlers/Image/ImageHandler.Windows.cs | 6 ++ .../Image/ImageHandlerTests.Windows.cs | 72 +++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/Core/src/Handlers/Image/ImageHandler.Windows.cs b/src/Core/src/Handlers/Image/ImageHandler.Windows.cs index aea2a86c90e8..759fe7ea4953 100644 --- a/src/Core/src/Handlers/Image/ImageHandler.Windows.cs +++ b/src/Core/src/Handlers/Image/ImageHandler.Windows.cs @@ -323,6 +323,12 @@ public override void SetImageSource(ImageSource? platformImage) return; image.Source = platformImage; + + // ImageOpened is raised only when WinUI decodes the source itself. A WriteableBitmap, or a + // BitmapImage that was already decoded with SetSourceAsync (file path, stream and URI sources), + // has its size right away and never raises it, so apply the AspectFit constraints now. + if (platformImage is BitmapSource { PixelWidth: > 0, PixelHeight: > 0 } && Handler is ImageHandler handler) + handler.UpdatePlatformMaxConstraints(); } } } diff --git a/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs b/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs index 748409a12070..c2e06e4e630e 100644 --- a/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs @@ -118,4 +118,76 @@ await AttachAndRun(platformView, async () => }); } } + + // These tests are only for ImageHandler: ImageButtonHandler does not cap the size of its image. + public partial class ImageHandlerTests + { + // https://github.com/dotnet/maui/issues/34968 + // FileImageSourceService loads a file path with BitmapImage.SetSourceAsync, so the bitmap is + // already decoded when it is set and WinUI does not raise ImageOpened for it. + [Fact] + public async Task AspectFit_ImageFromFilePath_PlatformMaxSizeIsConstrainedToPixelSize() + { + var image = new ImageStub + { + Aspect = Aspect.AspectFit, + HorizontalLayoutAlignment = LayoutAlignment.Center, + VerticalLayoutAlignment = LayoutAlignment.Center, + Source = new FileImageSourceStub(BaseImageSourceServiceTests.CreateBitmapFile(60, 30, Colors.Red)), + }; + + await InvokeOnMainThreadAsync(async () => + { + var handler = CreateHandler(image); + var platformView = handler.PlatformView; + + await AttachAndRun(platformView, async () => + { + await image.WaitUntilLoaded(); + + var bitmapSource = Assert.IsAssignableFrom(platformView.Source); + Assert.Equal(60, bitmapSource.PixelWidth); + Assert.Equal(30, bitmapSource.PixelHeight); + AssertMaxSizeIsPixelSize(platformView, bitmapSource); + }); + }); + } + + // https://github.com/dotnet/maui/issues/34968 + // A custom image source service can return a WriteableBitmap. It has its size as soon as it + // is created and WinUI does not raise ImageOpened for it. + [Fact] + public async Task AspectFit_WriteableBitmap_PlatformMaxSizeIsConstrainedToPixelSize() + { + var image = new ImageStub + { + Aspect = Aspect.AspectFit, + HorizontalLayoutAlignment = LayoutAlignment.Center, + VerticalLayoutAlignment = LayoutAlignment.Center, + }; + + await InvokeOnMainThreadAsync(async () => + { + var handler = CreateHandler(image); + var platformView = handler.PlatformView; + + await AttachAndRun(platformView, () => + { + // This is what the source loader does with the bitmap returned by such a service. + var bitmap = BaseImageSourceServiceTests.CreateBitmap(60, 30, Colors.Red); + handler.SourceLoader.Setter.SetImageSource(bitmap); + + AssertMaxSizeIsPixelSize(platformView, bitmap); + }); + }); + } + + static void AssertMaxSizeIsPixelSize(WImage platformView, BitmapSource bitmap) + { + Assert.True(platformView.MaxWidth <= bitmap.PixelWidth + 0.5, + $"MaxWidth ({platformView.MaxWidth:F0}) should be <= PixelWidth ({bitmap.PixelWidth})."); + Assert.True(platformView.MaxHeight <= bitmap.PixelHeight + 0.5, + $"MaxHeight ({platformView.MaxHeight:F0}) should be <= PixelHeight ({bitmap.PixelHeight})."); + } + } } From 17c7af340a78ad6d1e38ea136b082d4850ddf449 Mon Sep 17 00:00:00 2001 From: Empiree Date: Wed, 14 Oct 2026 17:52:44 +0200 Subject: [PATCH 2/2] Fix Windows bitmap file helper used by the new test The png encoder was never flushed, so CreateBitmapFile wrote an empty file on Windows. It also creates a WriteableBitmap, so the test now calls it on the UI thread. --- .../DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs | 4 +++- .../ImageSource/BaseImageSourceServiceTests.Windows.cs | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs b/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs index c2e06e4e630e..ebce8a09c528 100644 --- a/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs @@ -133,11 +133,13 @@ public async Task AspectFit_ImageFromFilePath_PlatformMaxSizeIsConstrainedToPixe Aspect = Aspect.AspectFit, HorizontalLayoutAlignment = LayoutAlignment.Center, VerticalLayoutAlignment = LayoutAlignment.Center, - Source = new FileImageSourceStub(BaseImageSourceServiceTests.CreateBitmapFile(60, 30, Colors.Red)), }; await InvokeOnMainThreadAsync(async () => { + // CreateBitmapFile makes a WriteableBitmap, so it has to run on the UI thread. + image.Source = new FileImageSourceStub(BaseImageSourceServiceTests.CreateBitmapFile(60, 30, Colors.Red)); + var handler = CreateHandler(image); var platformView = handler.PlatformView; diff --git a/src/Core/tests/DeviceTests/Services/ImageSource/BaseImageSourceServiceTests.Windows.cs b/src/Core/tests/DeviceTests/Services/ImageSource/BaseImageSourceServiceTests.Windows.cs index 792aee1a4dae..b96816e681ef 100644 --- a/src/Core/tests/DeviceTests/Services/ImageSource/BaseImageSourceServiceTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Services/ImageSource/BaseImageSourceServiceTests.Windows.cs @@ -54,6 +54,9 @@ public static Stream CreateBitmapStream(int width, int height, WColor color) 96, bitmap.PixelBuffer.ToArray()); + // The encoder writes nothing to the stream until it is flushed. + encoder.FlushAsync().GetAwaiter().GetResult(); + stream.Position = 0; return stream;