@@ -12,7 +12,6 @@ namespace CommunityToolkit.WinUI.Controls.TextElements;
1212internal class MyImage : IAddChild
1313{
1414 private InlineUIContainer _container = new InlineUIContainer ( ) ;
15- private Border _border = new Border ( ) ;
1615 private LinkInline ? _linkInline ;
1716 private Image _image = new Image ( ) ;
1817 private Uri _uri ;
@@ -85,8 +84,7 @@ out var height
8584 private void Init ( )
8685 {
8786 _image . Loaded += LoadImage ;
88- _border . Child = _image ;
89- _container . Child = _border ;
87+ _container . Child = _image ;
9088 }
9189
9290 private async void LoadImage ( object sender , RoutedEventArgs e )
@@ -97,7 +95,7 @@ private async void LoadImage(object sender, RoutedEventArgs e)
9795 if ( _imageProvider != null && _imageProvider . ShouldUseThisProvider ( _uri . AbsoluteUri ) )
9896 {
9997 _image = await _imageProvider . GetImage ( _uri . AbsoluteUri ) ;
100- _border . Child = _image ;
98+ _container . Child = _image ;
10199 }
102100 else
103101 {
@@ -121,7 +119,7 @@ private async void LoadImage(object sender, RoutedEventArgs e)
121119 if ( resImage != null )
122120 {
123121 _image = resImage ;
124- _border . Child = _image ;
122+ _container . Child = _image ;
125123 }
126124 }
127125 else
@@ -139,57 +137,39 @@ private async void LoadImage(object sender, RoutedEventArgs e)
139137 await bitmap . SetSourceAsync ( stream ) ;
140138 }
141139 _image . Source = bitmap ;
142- _image . Width = bitmap . PixelWidth == 0 ? bitmap . DecodePixelWidth : bitmap . PixelWidth ;
143- _image . Height = bitmap . PixelHeight == 0 ? bitmap . DecodePixelHeight : bitmap . PixelHeight ;
144-
140+ // Don't set fixed Width/Height - let layout system handle it
141+ // Store natural dimensions for MaxWidth/MaxHeight constraints
142+ double naturalWidth = bitmap . PixelWidth == 0 ? bitmap . DecodePixelWidth : bitmap . PixelWidth ;
143+ double naturalHeight = bitmap . PixelHeight == 0 ? bitmap . DecodePixelHeight : bitmap . PixelHeight ;
144+
145+ // Use natural size as max constraint so image doesn't upscale
146+ _image . MaxWidth = naturalWidth ;
147+ _image . MaxHeight = naturalHeight ;
145148 }
146149
147150 _loaded = true ;
148151 }
149152
150- // Determine the actual image dimensions
151- double actualWidth = _precedentWidth != 0 ? _precedentWidth : _image . Width ;
152- double actualHeight = _precedentHeight != 0 ? _precedentHeight : _image . Height ;
153-
154- // Apply max constraints and calculate the final size
155- // When using Uniform stretch with max constraints, we need to calculate
156- // the actual rendered size to avoid gaps
157- double finalWidth = actualWidth ;
158- double finalHeight = actualHeight ;
159-
160- bool hasMaxWidth = _themes . ImageMaxWidth > 0 ;
161- bool hasMaxHeight = _themes . ImageMaxHeight > 0 ;
162-
163- if ( hasMaxWidth || hasMaxHeight )
153+ // Apply precedent (markdown-specified) dimensions if provided
154+ if ( _precedentWidth != 0 )
164155 {
165- double scaleX = hasMaxWidth && actualWidth > _themes . ImageMaxWidth
166- ? _themes . ImageMaxWidth / actualWidth
167- : 1.0 ;
168- double scaleY = hasMaxHeight && actualHeight > _themes . ImageMaxHeight
169- ? _themes . ImageMaxHeight / actualHeight
170- : 1.0 ;
171-
172- // For Uniform stretch, use the smaller scale to maintain aspect ratio
173- if ( _themes . ImageStretch == Stretch . Uniform || _themes . ImageStretch == Stretch . UniformToFill )
174- {
175- double uniformScale = Math . Min ( scaleX , scaleY ) ;
176- finalWidth = actualWidth * uniformScale ;
177- finalHeight = actualHeight * uniformScale ;
178- }
179- else
180- {
181- // For other stretch modes, apply constraints independently
182- finalWidth = actualWidth * scaleX ;
183- finalHeight = actualHeight * scaleY ;
184- }
156+ _image . MaxWidth = _precedentWidth ;
157+ }
158+ if ( _precedentHeight != 0 )
159+ {
160+ _image . MaxHeight = _precedentHeight ;
185161 }
186162
187- _image . Width = finalWidth ;
188- _image . Height = finalHeight ;
163+ // Apply theme constraints - these override natural/precedent if smaller
164+ if ( _themes . ImageMaxWidth > 0 && _themes . ImageMaxWidth < _image . MaxWidth )
165+ {
166+ _image . MaxWidth = _themes . ImageMaxWidth ;
167+ }
168+ if ( _themes . ImageMaxHeight > 0 && _themes . ImageMaxHeight < _image . MaxHeight )
169+ {
170+ _image . MaxHeight = _themes . ImageMaxHeight ;
171+ }
189172 _image . Stretch = _themes . ImageStretch ;
190-
191- // Apply corner radius via the border
192- _border . CornerRadius = _themes . ImageCornerRadius ;
193173 }
194174 catch ( Exception ) { }
195175 }
0 commit comments