Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Core/src/Handlers/Image/ImageHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
Empiree marked this conversation as resolved.
Resolved
{
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<BitmapSource>(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}).");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading