Skip to content

Commit

Permalink
Fix CanvasImageSource scaling behavior (#21239)
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
  • Loading branch information
Foda and Mike Corsaro authored Mar 18, 2024
1 parent 33b7f79 commit b867df9
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Core/src/Platform/Windows/ButtonExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
using Microsoft.Graphics.Canvas.UI.Xaml;
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -159,10 +160,18 @@ public static void UpdateImageSource(this Button platformButton, WImageSource? n
{
if (platformButton.GetContent<WImage>() is WImage nativeImage)
{
// Stretch to the size of the button
// Stretch to fill
nativeImage.Stretch = UI.Xaml.Media.Stretch.Uniform;
nativeImage.Source = nativeImageSource;

// If we're a CanvasImageSource (font image source), we need to explicitly set the image height
// to the desired size of the font, otherwise it will be stretched to the available space
if (nativeImage.Source is CanvasImageSource canvas)
{
nativeImage.Width = canvas.Size.Width;
nativeImage.Height = canvas.Size.Height;
}

nativeImage.Visibility = nativeImageSource == null
? UI.Xaml.Visibility.Collapsed
: UI.Xaml.Visibility.Visible;
Expand Down

0 comments on commit b867df9

Please sign in to comment.