Browse Source

fix tilebrush scaling for direct2d.

Dan Walmsley 7 years ago
parent
commit
1ced34f15f

+ 9 - 5
src/Avalonia.Visuals/Rendering/Utilities/TileBrushCalculator.cs

@@ -9,6 +9,7 @@ namespace Avalonia.Rendering.Utilities
     {
         private readonly Size _imageSize;
         private readonly Rect _drawRect;
+        private readonly Vector _dpi;
 
         public bool IsValid { get; }
 
@@ -18,7 +19,7 @@ namespace Avalonia.Rendering.Utilities
         /// <param name="brush">The brush to be rendered.</param>
         /// <param name="contentSize">The size of the content of the tile brush.</param>
         /// <param name="targetSize">The size of the control to which the brush is being rendered.</param>
-        public TileBrushCalculator(ITileBrush brush, Size contentSize, Size targetSize)
+        public TileBrushCalculator(ITileBrush brush, Size contentSize, Size targetSize, Vector dpi)
             : this(
                   brush.TileMode,
                   brush.Stretch,
@@ -27,7 +28,8 @@ namespace Avalonia.Rendering.Utilities
                   brush.SourceRect,
                   brush.DestinationRect,
                   contentSize,
-                  targetSize)
+                  targetSize,
+                  dpi)
         {
         }
 
@@ -50,12 +52,14 @@ namespace Avalonia.Rendering.Utilities
             RelativeRect sourceRect,
             RelativeRect destinationRect,
             Size contentSize,
-            Size targetSize)
+            Size targetSize,
+            Vector dpi)
         {
             _imageSize = contentSize;
+            _dpi = dpi;
 
-            SourceRect = sourceRect.ToPixels(_imageSize);
-            DestinationRect = destinationRect.ToPixels(targetSize);
+            SourceRect = sourceRect.ToPixels(_imageSize) * (_dpi / 96);
+            DestinationRect = destinationRect.ToPixels(targetSize) * (dpi / 96);
 
             var scale = stretch.CalculateScaling(DestinationRect.Size, SourceRect.Size);
             var translate = CalculateTranslate(alignmentX, alignmentY, SourceRect, DestinationRect, scale);

+ 2 - 2
src/Windows/Avalonia.Direct2D1/Media/ImageBrushImpl.cs

@@ -20,8 +20,8 @@ namespace Avalonia.Direct2D1.Media
             BitmapImpl bitmap,
             Size targetSize)
         {
-            var calc = new TileBrushCalculator(brush, bitmap.PixelSize.ToSize(96), targetSize);
-
+            var calc = new TileBrushCalculator(brush, bitmap.PixelSize.ToSize(96), targetSize, new Vector(target.DotsPerInch.Width, target.DotsPerInch.Height));
+             
             if (!calc.NeedsIntermediate)
             {
                 _bitmap = bitmap.GetDirect2DBitmap(target);