diff --git a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs index 6a29fd728f..f82cc3cdcd 100644 --- a/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs +++ b/src/Gtk/Perspex.Cairo/Media/DrawingContext.cs @@ -42,6 +42,9 @@ namespace Perspex.Cairo.Media CurrentTransform = Matrix.Identity; } + /// + /// Gets the current transform of the drawing context. + /// public Matrix CurrentTransform { get; } @@ -54,17 +57,30 @@ namespace Perspex.Cairo.Media _context.Dispose(); } + /// + /// Draws a bitmap image. + /// + /// The bitmap image. + /// The opacity to draw with. + /// The rect in the image to draw. + /// The rect in the output to draw to. public void DrawImage(IBitmap bitmap, double opacity, Rect sourceRect, Rect destRect) { var impl = bitmap.PlatformImpl as BitmapImpl; var size = new Size(impl.PixelWidth, impl.PixelHeight); - var scaleX = destRect.Size.Width / sourceRect.Size.Width; - var scaleY = destRect.Size.Height / sourceRect.Size.Height; + var scale = new Vector(destRect.Width / sourceRect.Width, destRect.Height / sourceRect.Height); _context.Save(); - _context.Scale(scaleX, scaleY); - Gdk.CairoHelper.SetSourcePixbuf(_context, impl.Surface, (int)sourceRect.X, (int)sourceRect.Y); - _context.Rectangle(sourceRect.ToCairo()); + _context.Scale(scale.X, scale.Y); + destRect /= scale; + + Gdk.CairoHelper.SetSourcePixbuf( + _context, + impl.Surface, + -sourceRect.X + destRect.X, + -sourceRect.Y + destRect.Y); + + _context.Rectangle(destRect.ToCairo()); _context.Fill(); _context.Restore(); } diff --git a/src/Perspex.SceneGraph/Rect.cs b/src/Perspex.SceneGraph/Rect.cs index 95b08eedd6..2dba97e764 100644 --- a/src/Perspex.SceneGraph/Rect.cs +++ b/src/Perspex.SceneGraph/Rect.cs @@ -184,15 +184,11 @@ namespace Perspex /// The scaled rectangle. public static Rect operator *(Rect rect, Vector scale) { - double centerX = rect._x + (rect._width / 2); - double centerY = rect._y + (rect._height / 2); - double width = rect._width * scale.X; - double height = rect._height * scale.Y; return new Rect( - centerX - (width / 2), - centerY - (height / 2), - width, - height); + rect.X * scale.X, + rect.Y * scale.Y, + rect.Width * scale.X, + rect.Height * scale.Y); } /// @@ -214,15 +210,11 @@ namespace Perspex /// The scaled rectangle. public static Rect operator /(Rect rect, Vector scale) { - double centerX = rect._x + (rect._width / 2); - double centerY = rect._y + (rect._height / 2); - double width = rect._width / scale.X; - double height = rect._height / scale.Y; return new Rect( - centerX - (width / 2), - centerY - (height / 2), - width, - height); + rect.X / scale.X, + rect.Y / scale.Y, + rect.Width / scale.X, + rect.Height / scale.Y); } ///