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..ebce8a09c528 100644 --- a/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs +++ b/src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.cs @@ -118,4 +118,78 @@ 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, + }; + + 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; + + 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})."); + } + } } 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;