如何在 C# 中缩放图片框中的某个点?

Qua*_*ema 2 c# picturebox zooming winforms

这个问题之前曾被问过,但由于它不起作用并且我缺乏声誉点(我试图对问题发表评论,但我不能),我不得不再次问这个问题。

这是之前问的问题的链接; 如何缩放图片框中的某个点

我使用了链接中显示的代码,但是当我运行它时,点或形状消失了。

这是我的代码;

public partial class Form1 : Form
{
    private Matrix transform = new Matrix();     
    private double m_dZoomscale = 1.0;    
    public static double s_dScrollValue = .1;
 }
private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.Transform = transform;
        Pen mypen = new Pen(Color.Red,5);
        Rectangle rect = new Rectangle(10, 10, 30, 30);
        e.Graphics.DrawRectangle(mypen, rect);
    }
    protected override void OnMouseWheel(MouseEventArgs mea)
    {
        pictureBox1.Focus();
        if (pictureBox1.Focused == true && mea.Delta != 0)
        {
            ZoomScroll(mea.Location, mea.Delta > 0);
        }
    }
      private void ZoomScroll(Point location, bool zoomIn)
    {
        transform.Translate(-location.X, -location.Y);
        if (zoomIn)
            transform.Scale((float)s_dScrollValue, (float)s_dScrollValue);
        else
            transform.Scale((float)-s_dScrollValue, (float)-s_dScrollValue);

        transform.Translate(location.X, location.Y);

        pictureBox1.Invalidate();
    }
Run Code Online (Sandbox Code Playgroud)

Pet*_*iho 8

您引用的答案不可能有效。我不知道为什么它被接受,也不被投票。除了在过去的某个时候,显然也投了赞成票。我不知道我在想什么。

无论如何,该代码有一些问题:

  1. 它使用直接传入的鼠标坐标,而不是将它们转换为控件的坐标系PictureBox。传递给该方法的坐标OnMouseWheel()是相对于其Form自身的,因此只有当PictureBox左上角与 的Form左上角重合时才有效。
  2. 更成问题的是,代码完全滥用了该Matrix.Scale()方法,传递了一个似乎旨在作为比例增量的值,而实际上该Scale()方法接受比例因子。这有两个含义:
    • 传递负值是错误的,因为负值会翻转坐标系,而不是缩小比例,并且
    • 传递增量值是错误的,因为传递的值将当前缩放比例相乘以获得新的缩放比例。
  3. 还有一个问题是,代码以错误的顺序应用矩阵变换,因为默认顺序是“前置”,而不是“附加”(我发现后者更自然,但我认为专业人士知道有一些原因)在矩阵数学中解释了为什么默认值是前者)。

还有一个相对较小的问题,即使忽略上述内容,允许用户任意调整比例因子最终也会导致值超出范围。代码最好将规模限制在合理的范围内。

这是您的代码的一个版本,经过修改以解决所有这些问题:

private Matrix transform = new Matrix();
private float m_dZoomscale = 1.0f;
public const float s_dScrollValue = 0.1f;

public Form1()
{
    InitializeComponent();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.Transform = transform;
    Pen mypen = new Pen(Color.Red, 5);
    Rectangle rect = new Rectangle(10, 10, 30, 30);
    e.Graphics.DrawRectangle(mypen, rect);
}

protected override void OnMouseWheel(MouseEventArgs mea)
{
    pictureBox1.Focus();
    if (pictureBox1.Focused == true && mea.Delta != 0)
    {
        // Map the Form-centric mouse location to the PictureBox client coordinate system
        Point pictureBoxPoint = pictureBox1.PointToClient(this.PointToScreen(mea.Location));
        ZoomScroll(pictureBoxPoint, mea.Delta > 0);
    }
}

private void ZoomScroll(Point location, bool zoomIn)
{
    // Figure out what the new scale will be. Ensure the scale factor remains between
    // 1% and 1000%
    float newScale = Math.Min(Math.Max(m_dZoomscale + (zoomIn ? s_dScrollValue : -s_dScrollValue), 0.1f), 10);

    if (newScale != m_dZoomscale)
    {
        float adjust = newScale / m_dZoomscale;
        m_dZoomscale = newScale;

        // Translate mouse point to origin
        transform.Translate(-location.X, -location.Y, MatrixOrder.Append);

        // Scale view
        transform.Scale(adjust, adjust, MatrixOrder.Append);

        // Translate origin back to original mouse point.
        transform.Translate(location.X, location.Y, MatrixOrder.Append);

        pictureBox1.Invalidate();
    }
}
Run Code Online (Sandbox Code Playgroud)

通过这段代码,您会发现,在调整鼠标滚轮之前,无论将鼠标放在哪里,渲染的图像都会缩放,同时保持鼠标下方的点固定到位。


笔记:

我查看了 Stack Overflow 上的一些类似问题,其中有一些可能对您也有用。在我看来,有些答案使事情变得过于复杂,但一切都应该有效。看:

缩放到点未按预期工作
使用矩阵
放大固定点 缩放图形而不滚动