@@ -27,7 +27,10 @@ public static BitmapImage Resize(this BitmapImage image, Size size)
2727 {
2828 if ( image is SkiaSharpBitmap img )
2929 {
30- var resized = img . WrappedBitmap . Resize ( new SKSizeI ( size . Width , size . Height ) , SKFilterQuality . Medium ) ;
30+ // SKFilterQuality.Medium maps to a linear filter with a linear mipmap.
31+ // See https://api.skia.org/SkSamplingOptions_8h_source.html
32+ var sampling = new SKSamplingOptions ( SKFilterMode . Linear , SKMipmapMode . Linear ) ;
33+ var resized = img . WrappedBitmap . Resize ( new SKSizeI ( size . Width , size . Height ) , sampling ) ;
3134 return new SkiaSharpBitmap ( resized , image . PixelFormat ) ;
3235 }
3336
@@ -77,11 +80,15 @@ public static void FillRectangle(this IGraphics graphics, Rectangle rectangle, C
7780 public static SizeF MeasureText ( this IGraphics graphics , string text , string fontFamilyName , int size )
7881 {
7982 var canvas = GetCanvas ( graphics ) ;
80- SKFont fnt = new SKFont ( SKTypeface . FromFamilyName ( fontFamilyName ) , size ) ;
81- var paint = new SKPaint ( fnt ) ;
82- paint . TextAlign = SKTextAlign . Left ;
83- paint . TextEncoding = SKTextEncoding . Utf16 ;
84- float width = paint . MeasureText ( text ) ;
83+ using SKTypeface typeface = SKTypeface . FromFamilyName ( fontFamilyName ) ;
84+ using SKFont fnt = new SKFont ( typeface , size ) ;
85+ // Match the previous behavior of SKPaint(fnt).MeasureText(text). The obsolete SKPaint
86+ // text path defaulted IsAntialias=false, so we mirror that by disabling font edging.
87+ // The string overload of MeasureText handles UTF-16 (the natural encoding of managed
88+ // strings), matching TextEncoding=Utf16; the previous TextAlign=Left did not affect
89+ // the returned width.
90+ fnt . Edging = SKFontEdging . Alias ;
91+ float width = fnt . MeasureText ( text ) ;
8592 return new SizeF ( width , size ) ;
8693 }
8794
@@ -91,11 +98,14 @@ public static SizeF MeasureText(this IGraphics graphics, string text, string fon
9198 public static void DrawText ( this IGraphics graphics , string text , string fontFamilyName , int size , Color color , Point position )
9299 {
93100 var canvas = GetCanvas ( graphics ) ;
94- SKFont fnt = new SKFont ( SKTypeface . FromFamilyName ( fontFamilyName ) , size ) ;
95- var paint = new SKPaint ( fnt ) ;
101+ using SKTypeface typeface = SKTypeface . FromFamilyName ( fontFamilyName ) ;
102+ using SKFont fnt = new SKFont ( typeface , size ) ;
103+ // Match the previous behavior of canvas.DrawText(t, pt, new SKPaint(fnt) { ... }).
104+ // The obsolete SKPaint text path defaulted IsAntialias=false, which produced aliased
105+ // glyphs; mirror that by disabling font edging so output is pixel-identical.
106+ fnt . Edging = SKFontEdging . Alias ;
107+ using var paint = new SKPaint ( ) ;
96108 paint . Color = new SKColor ( ( uint ) color . ToArgb ( ) ) ;
97- paint . TextAlign = SKTextAlign . Left ;
98- paint . TextEncoding = SKTextEncoding . Utf16 ;
99109 int lineSpacing = size + 2 ;
100110 SKPoint currentPosition = new SKPoint ( position . X , position . Y + size ) ; // drawing begins to the right and above the given point.
101111 var texts = text . Split ( new char [ ]
@@ -106,7 +116,9 @@ public static void DrawText(this IGraphics graphics, string text, string fontFam
106116 // The DrawText implementation of SkiaSharp does not work with line breaks, so do that manually.
107117 foreach ( var t in texts )
108118 {
109- canvas . DrawText ( t , currentPosition , paint ) ;
119+ // The string + SKFont overload uses UTF-16 encoding (natural for managed strings),
120+ // matching the previous TextEncoding=Utf16. SKTextAlign.Left preserves the original alignment.
121+ canvas . DrawText ( t , currentPosition , SKTextAlign . Left , fnt , paint ) ;
110122 currentPosition . Y += lineSpacing ;
111123 }
112124 }
0 commit comments