/// <summary> /// Zoom to a given rectangle. /// The rectangle is in local space. /// </summary> public override void ZoomToRectangle(RectangleF rectangle) { using (var update = canvas.BeginUpdate()) { var origRect = rectangle; // Adjust (correct) rectangle Size sz = Global2Local(this.Size); if (rectangle.Left < 0) { rectangle.Offset(-rectangle.Left, 0); } if (rectangle.Top < 0) { rectangle.Offset(0, -rectangle.Top); } rectangle.Width = Math.Min(rectangle.Width, sz.Width); rectangle.Height = Math.Min(rectangle.Height, sz.Height); Size panelSize = LayoutManager.ClientSize(canvas.PanelSize); float h = (float)panelSize.Width / rectangle.Width; float v = (float)panelSize.Height / rectangle.Height; canvas.ZoomFactor = Math.Min(h, v); panelSize = canvas.PanelSize; var zoomFactor = canvas.ZoomFactor; float localPanelWidth = panelSize.Width / zoomFactor; float localPanelHeight = panelSize.Height / zoomFactor; float dx = (localPanelWidth - rectangle.Width) / 2; float dy = (localPanelHeight - rectangle.Height) / 2; canvas.VisibleLeft = (int)(rectangle.Left - dx); canvas.VisibleTop = (int)(rectangle.Top - dy); } }