The math behind MultiScaleImage.ZoomAboutLogicalPoint

Developing a Silverlight DeepZoom app, I needed to reimplement MultiScaleImage.ZoomAboutLogicalPoint method in terms of changing ViewportWidth and ViewportOrigin, and this is what code I’ve ended with:

public void ZoomAboutPoint(double newScale, Point scalePoint)
 double newViewportWidth = _currentViewportWidth / newScale;
 Point centerPoint = new Point(scalePoint.X / this.ActualWidth, scalePoint.Y / this.ActualHeight);
 double aboutLogicalPointFactor = _currentViewportWidth - newViewportWidth;
 Point newViewportOrigin = new Point(
   _currentViewportOrigin.X + aboutLogicalPointFactor * centerPoint.X,
   _currentViewportOrigin.Y + aboutLogicalPointFactor * centerPoint.Y / ControlAspectRatio);

You pass a scale factor (the same as you would for ZoomAboutLogicalPoint) and the scalePoint, which is in coordinate system of your control (for example, retrieved with GetPosition() from a mouse event handler).

UPD.: Added control aspect ration to the last line. Thanks, Benny!

Leave a comment