有三种典型的缩放类型:
- 放大到中心,由缩放按钮触发
- 放大鼠标位置,通过点击或滚轮触发
- 通过绘制矩形放大矩形
我假设典型设置:将PictureBox 设置为SizeMode=Zoom 嵌套 在带有Panel 的AutoScroll=true 和注意保持纵横比 的缩放Image 和 PictureBox 的强>相等。
让我们从介绍术语开始:
- 有一个
Image,我们称之为位图和
- 显示为
PictureBox;我们称之为 canvas..
- .. 嵌套在
Panel 中,我们称之为 frame
用户友好的缩放需要一个固定点,这个点应该保持不变。
对于 1) 它是 frame 的中心,对于 2) 它是鼠标位置,对于 3) 它是矩形的中心。
在缩放之前,我们计算旧的缩放比例,帧中的不动点,中的不动点>canvas,最后是位图中的固定点。
缩放后,我们计算新的缩放比例和画布中的新固定点。最后我们用它来移动canvas,将固定的canvas point带到固定的frame point。 p>
这是放大(当前)中心的示例;这是两个按钮的常见点击事件,它只会将缩放比例加倍和减半。
更细粒度的因素当然很容易实现;更好的是一个固定的缩放级别列表,就像 Photoshop 一样!
private void zoom_Click(object sender, EventArgs e)
{
PictureBox canvas = pictureBox1;
Panel frame = panel1;
// Set new zoom level, depending on the button
float zoom = sender == btn_ZoomIn ? 2f : 0.5f;
// calculate old ratio:
float ratio = 1f * canvas.ClientSize.Width / canvas.Image.Width;
// calculate frame fixed pixel:
Point fFix = new Point( frame.Width / 2, frame.Height / 2);
// calculate the canvas fixed pixel:
Point cFix = new Point(-canvas.Left + fFix.X, -canvas.Top + fFix.Y );
// calculate the bitmap fixed pixel:
Point iFix = new Point((int)(cFix.X / ratio),(int)( cFix.Y / ratio));
// do the zoom
canvas.Size = new Size( (int)(canvas.Width * zoom), (int)(canvas.Height * zoom) );
// calculate new ratio:
float ratio2 = 1f * canvas.ClientSize.Width / canvas.Image.Width;
// calculate the new canvas fixed pixel:
Point cFix2 = new Point((int)(iFix.X * ratio2),(int)( iFix.Y * ratio2));
// move the canvas:
canvas.Location = new Point(-cFix2.X + fFix.X, -cFix2.Y + fFix.Y);
}
注意虽然可以尝试恢复相对的AutoScrollValues,但这不仅很难,因为它们的值有点古怪,而且也不适用于其他缩放类型.