I have made something like a drawing app but I need to implement a function to zoom in/out with using the right mouse button. I have added zooming but it zooms at the left-top corner. I tried to zoom in at given coordinates but neither of tries were successful.
float zoom = 1f;
private void canvasPanel_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.ScaleTransform(zoom, zoom);
DrawShapesFromListOnPaint(e);
}
// ...
private void canvasPanel_MouseClick(object sender, MouseEventArgs e)
{ // For Right Click Zooming
switch (e.Button)
{
case MouseButtons.Right:
{
if (zoomed)
{
zoom -= 1;
zoomed = false;
}
else
{
zoom += 1;
zoomed = true;
zoomPoint = canvasPanel.PointToClient(Cursor.Position);
}
canvasPanel.Invalidate();
}
break;
case MouseButtons.Left:
// ...
break;
}
}
As Hans Passant said, you also need a TranslateTransform() call. I wrote an example, left click to zoom in, right click to zoom out, please refer to:
The effect is as follows: