diff --git a/Greenshot/Drawing/Adorners/AbstractAdorner.cs b/Greenshot/Drawing/Adorners/AbstractAdorner.cs
index fc3d72371..51ae7f14f 100644
--- a/Greenshot/Drawing/Adorners/AbstractAdorner.cs
+++ b/Greenshot/Drawing/Adorners/AbstractAdorner.cs
@@ -1,4 +1,4 @@
-/*
+/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
@@ -123,13 +123,8 @@ namespace Greenshot.Drawing.Adorners
{
get
{
- Point[] points = { Location };
- var matrix = Owner.Parent.ZoomMatrix;
- if (!matrix.IsIdentity)
- {
- matrix.TransformPoints(points);
- }
- return new Rectangle(points[0].X - _size.Width / 2, points[0].Y - _size.Height / 2, _size.Width, _size.Height);
+ Point displayLocation = Owner.Parent.ToSurfaceCoordinates(Location);
+ return new Rectangle(displayLocation.X - _size.Width / 2, displayLocation.Y - _size.Height / 2, _size.Width, _size.Height);
}
}
diff --git a/Greenshot/Drawing/Adorners/TargetAdorner.cs b/Greenshot/Drawing/Adorners/TargetAdorner.cs
index 23cf29d09..4011754f7 100644
--- a/Greenshot/Drawing/Adorners/TargetAdorner.cs
+++ b/Greenshot/Drawing/Adorners/TargetAdorner.cs
@@ -1,4 +1,4 @@
-/*
+/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
@@ -61,26 +61,26 @@ namespace Greenshot.Drawing.Adorners
Owner.Invalidate();
Point newGripperLocation = new Point(mouseEventArgs.X, mouseEventArgs.Y);
- Rectangle surfaceBounds = new Rectangle(0, 0, Owner.Parent.Width, Owner.Parent.Height);
+ Rectangle imageBounds = new Rectangle(0, 0, Owner.Parent.Image.Width, Owner.Parent.Image.Height);
// Check if gripper inside the parent (surface), if not we need to move it inside
// This was made for BUG-1682
- if (!surfaceBounds.Contains(newGripperLocation))
+ if (!imageBounds.Contains(newGripperLocation))
{
- if (newGripperLocation.X > surfaceBounds.Right)
+ if (newGripperLocation.X > imageBounds.Right)
{
- newGripperLocation.X = surfaceBounds.Right - 5;
+ newGripperLocation.X = imageBounds.Right - 5;
}
- if (newGripperLocation.X < surfaceBounds.Left)
+ if (newGripperLocation.X < imageBounds.Left)
{
- newGripperLocation.X = surfaceBounds.Left;
+ newGripperLocation.X = imageBounds.Left;
}
- if (newGripperLocation.Y > surfaceBounds.Bottom)
+ if (newGripperLocation.Y > imageBounds.Bottom)
{
- newGripperLocation.Y = surfaceBounds.Bottom - 5;
+ newGripperLocation.Y = imageBounds.Bottom - 5;
}
- if (newGripperLocation.Y < surfaceBounds.Top)
+ if (newGripperLocation.Y < imageBounds.Top)
{
- newGripperLocation.Y = surfaceBounds.Top;
+ newGripperLocation.Y = imageBounds.Top;
}
}
diff --git a/Greenshot/Drawing/DrawableContainerList.cs b/Greenshot/Drawing/DrawableContainerList.cs
index 17b879538..9451b06ea 100644
--- a/Greenshot/Drawing/DrawableContainerList.cs
+++ b/Greenshot/Drawing/DrawableContainerList.cs
@@ -523,9 +523,9 @@ namespace Greenshot.Drawing {
}
}
- public virtual void ShowContextMenu(MouseEventArgs e, ISurface surface)
+ public virtual void ShowContextMenu(MouseEventArgs e, ISurface iSurface)
{
- if (!(surface is Surface))
+ if (!(iSurface is Surface surface))
{
return;
}
@@ -542,8 +542,7 @@ namespace Greenshot.Drawing {
ContextMenuStrip menu = new ContextMenuStrip();
AddContextMenuItems(menu, surface);
if (menu.Items.Count > 0) {
- // TODO: cast should be somehow avoided
- menu.Show((Surface)surface, e.Location);
+ menu.Show(surface, surface.ToSurfaceCoordinates(e.Location));
while (true) {
if (menu.Visible) {
Application.DoEvents();
diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs
index 5c38bea8e..0057a0838 100644
--- a/Greenshot/Drawing/Surface.cs
+++ b/Greenshot/Drawing/Surface.cs
@@ -1,4 +1,4 @@
-/*
+/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
*
@@ -302,6 +302,8 @@ namespace Greenshot.Drawing
}
}
+ [NonSerialized]
+ private Matrix _zoomMatrix = new Matrix(1, 0, 0, 1, 0, 0);
[NonSerialized]
private float _zoomFactor = 1.0f;
public float ZoomFactor
@@ -310,12 +312,11 @@ namespace Greenshot.Drawing
set
{
_zoomFactor = value;
- ZoomMatrix = new Matrix(_zoomFactor, 0, 0, _zoomFactor, 0, 0);
+ _zoomMatrix = new Matrix(_zoomFactor, 0, 0, _zoomFactor, 0, 0);
UpdateSize();
}
}
- public Matrix ZoomMatrix { get; private set; } = new Matrix(1, 0, 0, 1, 0, 0);
///
/// Sets the surface size as zoomed image size.
@@ -2108,5 +2109,26 @@ namespace Greenshot.Drawing
{
return _elements.Contains(container);
}
+
+ public Point ToSurfaceCoordinates(Point point)
+ {
+ Point[] points = { point };
+ _zoomMatrix.TransformPoints(points);
+ return points[0];
+ }
+
+ public Rectangle ToSurfaceCoordinates(Rectangle rc)
+ {
+ if (_zoomMatrix.IsIdentity)
+ {
+ return rc;
+ }
+ else
+ {
+ Point[] points = { rc.Location, rc.Location + rc.Size };
+ _zoomMatrix.TransformPoints(points);
+ return new Rectangle(points[0].X, points[0].Y, points[1].X - points[0].X, points[1].Y - points[1].Y);
+ }
+ }
}
}
diff --git a/Greenshot/Drawing/TextContainer.cs b/Greenshot/Drawing/TextContainer.cs
index bd4c75274..726ab5b7f 100644
--- a/Greenshot/Drawing/TextContainer.cs
+++ b/Greenshot/Drawing/TextContainer.cs
@@ -442,20 +442,15 @@ namespace Greenshot.Drawing
correction = -1;
}
Rectangle absRectangle = GuiRectangle.GetGuiRectangle(Left, Top, Width, Height);
- Point[] points = { absRectangle.Location, absRectangle.Location + absRectangle.Size };
- var matrix = Parent.ZoomMatrix;
- if (!matrix.IsIdentity)
- {
- matrix.TransformPoints(points);
- }
- _textBox.Left = points[0].X + lineWidth;
- _textBox.Top = points[0].Y + lineWidth;
+ Rectangle displayRectangle = Parent.ToSurfaceCoordinates(absRectangle);
+ _textBox.Left = displayRectangle.X + lineWidth;
+ _textBox.Top = displayRectangle.Y + lineWidth;
if (lineThickness <= 1)
{
lineWidth = 0;
}
- _textBox.Width = points[1].X - points[0].X - 2 * lineWidth + correction;
- _textBox.Height = points[1].Y - points[0].Y - 2 * lineWidth + correction;
+ _textBox.Width = displayRectangle.Width - 2 * lineWidth + correction;
+ _textBox.Height = displayRectangle.Height - 2 * lineWidth + correction;
}
public override void ApplyBounds(RectangleF newBounds)
diff --git a/GreenshotPlugin/Interfaces/ISurface.cs b/GreenshotPlugin/Interfaces/ISurface.cs
index 8ab44c810..a94415cbc 100644
--- a/GreenshotPlugin/Interfaces/ISurface.cs
+++ b/GreenshotPlugin/Interfaces/ISurface.cs
@@ -21,7 +21,6 @@
using System;
using System.Drawing;
-using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
using GreenshotPlugin.Effects;
@@ -200,9 +199,15 @@ namespace GreenshotPlugin.Interfaces
///
float ZoomFactor { get; set; }
///
- /// Matrix representing zoom applied to the surface.
+ /// Translate a point from image coorditate space to surface coordinate space.
///
- Matrix ZoomMatrix { get; }
+ /// A point in the coordinate space of the image.
+ Point ToSurfaceCoordinates(Point point);
+ ///
+ /// Translate a rectangle from image coorditate space to surface coordinate space.
+ ///
+ /// A rectangle in the coordinate space of the image.
+ Rectangle ToSurfaceCoordinates(Rectangle rc);
void MakeUndoable(IMemento memento, bool allowMerge);
}