这是我的代码 我可以缩放图片框,但不是一点。 旋转鼠标滚轮时如何缩放到鼠标点?
变量是: -
private double m_dZoomscale = 1.0; //THIS IS THE ZOOM SCALE TO WHICH EACH OBJECT
//ARE ZOOMED IN THE CANVAS
public static double s_dScrollValue = .01;
//scale factor value for mouse scroll zooming
油漆代码是: -
private void m_Picturebox_Canvas_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.ScaleTransform((float)m_dZoomscale, (float)m_dZoomscale);
}
主要代码是: -
protected override void OnMouseWheel(MouseEventArgs mea)
{
m_Picturebox_Canvas.Focus();
if (m_Picturebox_Canvas.Focused == true)
{
if (mea.Delta > 0)
{
ZoomInScroll();
}
else if (mea.Delta < 0)
{
ZoomOutScroll();
}
}
}
子功能如下: -
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomInScroll()
{
m_dZoomscale = m_dZoomscale + s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomOutScroll()
{
m_dZoomscale = m_dZoomscale - s_dScrollValue;
m_Picturebox_Canvas.Invalidate();
}
答案 0 :(得分:4)
以下是您如何实现此目标(下面的代码说明):
<强>变量强>
// this tracks the transformation applied to the PictureBox's Graphics
private Matrix transform = new Matrix();
public static double s_dScrollValue = 1.01; // zoom factor
绘制代码
private void m_Picturebox_Canvas_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Transform = transform;
}
滚动事件代码
protected override void OnMouseWheel(MouseEventArgs mea)
{
m_Picturebox_Canvas.Focus();
if (m_Picturebox_Canvas.Focused == true && mea.Delta != 0)
{
ZoomScroll(mea.Location, mea.Delta > 0);
}
}
缩放功能
//FUNCTION FOR MOUSE SCROL ZOOM-IN
private void ZoomScroll(Point location, bool zoomIn)
{
// make zoom-point (cursor location) our origin
transform.Translate(-location.X, -location.Y);
// perform zoom (at origin)
if(zoomIn)
transform.Scale(s_dScrollValue, s_dScrollValue);
else
transform.Scale(1 / s_dScrollValue, 1 / s_dScrollValue);
// translate origin back to cursor
transform.Translate(location.X, location.Y);
m_Picturebox_Canvas.Invalidate();
}
<强>描述强>
首先,如您所见,我将两种缩放方法合并为一种方法:ZoomScroll
否则我们会复制很多逻辑...
那么这里做了什么?我想很清楚我们还需要对Graphics
对象应用翻译。我们“累积”应用于PictureBox
字段中Matrix
的所有转换。
您成功缩放了图片,但始终将原点(PictureBox
的左上角)作为缩放操作的位置中心 - 这就是Scale
/ {{1工作!因此,为了在不同点进行扩展,需要执行以下步骤:
ScaleTransform
,因此我们翻译12|34
)< / LI>