简体   繁体   English

如何在c#中的图片框中放大一点?

[英]how to zoom at a point in picturebox in c#?

This question is asked before but since it doesn't work and my lack of reputation point(I tried to comment at question but I couldn't) I had to ask this question again. 这个问题之前被问过,但由于它不起作用而我的声誉不足(我试图评论但我不能)我不得不再次提出这个问题。

This is the link of the quustion asked before; 这是之前要求的quustion的链接; How to zoom at a point in picturebox 如何缩放图片框中的某个点

I used the code which is shown in the link but when I run it the point or shape disappear. 我使用链接中显示的代码,但是当我运行它时,点或形状消失。

here is my code; 这是我的代码;

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

The answer you are referencing cannot possibly work. 你引用的答案不可能奏效。 I have no idea why it was accepted, nor up-voted. 我不知道为什么它被接受,也没有被投票。 Except that at some time in the past, I apparently up-voted it as well. 除了在过去的某个时候, 显然也在投票。 I don't know what I was thinking. 我不知道我在想什么。

Anyway, that code has some problems: 无论如何,该代码存在一些问题:

  1. It uses the mouse coordinates passed in directly, rather than converting them to the coordinate system for the PictureBox control. 它使用直接传入的鼠标坐标,而不是将它们转换为PictureBox控件的坐标系。 The coordinates passed to the OnMouseWheel() method are relative to the Form itself, so only if the PictureBox top-left coincides with the Form 's upper-left corner would that work. 传递给OnMouseWheel()方法的坐标是相对于Form本身的,所以只有当PictureBox左上角与Form的左上角重合时才有效。
  2. More problematically, the code is completely misusing the Matrix.Scale() method, passing a value that seems intended to be a delta for the scale, when in fact the Scale() method accepts a factor for the scale. 更有问题的是,代码完全滥用Matrix.Scale()方法,传递一个似乎是比例尺的delta值,而实际上Scale()方法接受Scale() 因子 This has two implications: 这有两个含义:
    • Passing a negative value is wrong, because negative values flip the coordinate system, rather than reducing the scale, and 传递负值是错误的,因为负值会翻转坐标系,而不是缩小比例,而且
    • Passing an increment value is wrong, because the value passed will be multiplied with the current scaling to get the new scaling. 传递增量值是错误的,因为传递的值将当前缩放相乘以获得新的缩放。
  3. Also problematic is that the code applies the matrix transformations in the wrong order, because the default order is "prepend", not "append" (I find the latter more natural to work with, but I assume there's some reason known to those who specialize in matrix math that explains why the default is the former). 同样有问题的是代码以错误的顺序应用矩阵变换,因为默认顺序是“prepend”,而不是“append”(我发现后者更自然地使用,但我认为有一些原因让那些专业化的人知道在矩阵数学中解释了默认为前者的原因。

There is also the relatively minor issue that, even ignoring the above, allowing the user to adjust the scale factor arbitrarily will eventually lead to an out-of-range value. 还存在一个相对较小的问题,即使忽略上述内容,允许用户任意调整比例因子,最终也会导致超出范围的值。 It would be better for the code to limit the scale to something reasonable. 代码将规模限制在合理的范围内会更好。

Here is a version of your code, modified so that it addresses all of these issues: 以下是您的代码版本,经过修改后可以解决所有这些问题:

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

With this code, you will find that no matter where you place the mouse before adjusting the mouse wheel, the rendered image will scale while keeping the point under the mouse fixed in place. 使用此代码,您会发现无论您在调整鼠标滚轮之前放置鼠标的位置,渲染图像都会缩放,同时保持鼠标下方的点固定到位。


Note: 注意:

I took a look at some of the similar questions on Stack Overflow, and there are a few that might also be useful to you. 我看了一下Stack Overflow上的一些类似问题,还有一些可能对你有用。 Some of the answers overcomplicate things, in my opinion, but all should work. 在我看来,有些答案过于复杂,但一切都应该有效。 See: 看到:

Zoom To Point Not Working As Expected 缩放到点不按预期工作
Zoom in on a fixed point using matrices 使用矩阵放大固定点
Zooming graphics without scrolling 缩放图形而不滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM