编程语言
首页 > 编程语言> > c# – 如何缩放图片框中的某个点

c# – 如何缩放图片框中的某个点

作者:互联网

这是我的代码.
我可以缩放图片框,但不是一点.
旋转鼠标滚轮时如何缩放到鼠标点?

变量是: –

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 onm ouseWheel(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();
    }

解决方法:

以下是您如何实现这一点(下面的代码说明):

变量

// 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 onm ouseWheel(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对象应用转换.我们在Matrix字段中“累积”应用于PictureBox的所有变换.

您已成功缩放图像,但始终将原点(PictureBox的左上角)作为缩放操作的位置中心 – 这就是Scale / ScaleTransform的工作原理!因此,为了在不同的点进行扩展,需要执行以下步骤:

>翻译世界,以便你想要缩放的点是新的原点(例如你的光标是12 | 34,所以我们翻译为-12 | -34)
>现在,所需的地点是我们新的起源,规模
>将世界翻译回来,因此原点再次出现在光标下

标签:c,visual-studio-2010,c-4-0,zoom,mousewheel
来源: https://codeday.me/bug/20190624/1283166.html