Hi
I am writing a small drawing app in WPF. I am using DrawingVisual class to render shapes on Canvas.
I want to implement zoom In/Out for the Canvas control.
1) When I zoom in, zoom focus should be the mouse location.
2) When I zoom out, zoom focus should be the center of Canvas.
Below is the current code that I am trying but results are not satisfactory.
When I zoom out all shapes are moving towards the scaled/zoomed center location and not to the Canvas's actual center location.
So please guide me how should I compute the offset X and Y so that shapes will move towards center of canvas.
double zoomFactor = 1.1; double offsetX = 0; double offsetY = 0; protected override void OnMouseWheel(MouseWheelEventArgs e) { double absoluteMouseX; double absoluteMouseY; double centerX = this.ActualWidth / 2; //Canvas center X double centerY = this.ActualHeight / 2; // Canvas center Y Point relativeMouseToCanvas = e.GetPosition(this); if (e.Delta > 0) { absoluteMouseX = relativeMouseToCanvas.X * this.Zoom + offsetX; absoluteMouseY = relativeMouseToCanvas.Y * this.Zoom + offsetY; this.Zoom *= zoomFactor; offsetX = absoluteMouseX - relativeMouseToCanvas.X * this.Zoom; offsetY = absoluteMouseY - relativeMouseToCanvas.Y * this.Zoom; } if (e.Delta < 0) { absoluteMouseX = centerX * this.Zoom + offsetX; absoluteMouseY = centerY * this.Zoom + offsetY; this.Zoom /= zoomFactor; offsetX = absoluteMouseX - centerX * this.Zoom; offsetY = absoluteMouseY - centerY * this.Zoom; } // Refresh/Repaint shapes on canvas }Thanks