Problem - Before the zoom operation, the user can draw "points" according to the click position of the mouse on the canvas(picturebox
), but when the user uses the mouse wheel to zoom (without returning to the original zoom ratio), the user draws a "point" with the mouse clicke again, the clicked position of the mouse is inconsistent with the position of the drawn "point" on the canvas(picturebox
).
The demo code is down below:
private List<Point> _points;
private int _pointRadius = 60;
private float _zoom = 1f;
private float _offsetX = 0f;
private float _offsetY = 0f;
private void picturebox_MouseDown(object sender, MouseEventArgs e)
{
_points.Add(e.Location);
}
private void picturebox_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta < 0)
{
_zoom += 0.1f;
_offsetX = e.X * (1f - _zoom);
_offsetY = e.Y * (1f - _zoom);
}
else
{
if (_zoom <= 1f)
{
return;
}
_zoom -= 0.1f;
_offsetX = e.X * (1f - _zoom);
_offsetY = e.Y * (1f - _zoom);
}
picturebox.Invalidate();
}
private void picturebox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TranslateTransform(_offsetX, _offsetY);
e.Graphics.ScaleTransform(_zoom, _zoom);
foreach (Point point in _points)
{
e.Graphics.FillEllipse(Brushes.Black, point.X - _pointRadius, point.Y - _pointRadius, 2 * _pointRadius, 2 * _pointRadius);
}
}
[Edit1 - add the picturebox
properties screenshot]
[Edit2 - add a .gif to show the problem]
I would appreciate any help, it would be better if your answer could be more detailed.