0

我有一段代码在 Mouse_Up 时在 C# Windows 窗体上执行。它基本上会放大渲染图像,并在每次绘制橡皮筋时不断放大。我想通过添加一个复选框(我已经完成)来扭转这一点,如果勾选了该复选框,请根据橡皮筋的绘图从当前视图进行缩小。我认为这就像使用相反的操作数一样简单,即“+”而不是“-”,但它不会起作用。我已经在下面发布了事件代码。那里有一个条件 IF 用于更改复选框状态。第一部分可以很好地放大。ELSE IF 之后的部分不允许我缩小 - 因此我只是在那里留下了重复的代码供某人查看。谢谢

private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        if (checkBox1.Checked == false)
        {
            int z, w;
            this.Cursor = Cursors.Arrow;
            // Set internal flag to know we no longer "have the mouse".
            weHaveMouse = false;
            // If we have drawn previously, draw again in that spot
            // to remove the lines.
            if (ptLast.X != -1)
            {
                toolStripStatusLabel1.Text = "Please click and drag to zoom into the Fractal";
                Point ptCurrent = new Point(e.X, e.Y);
                MyDrawReversibleRectangle(ptOriginal, ptLast);
            }
            // Set flags to know that there is no "previous" line to reverse.
            ptLast.X = -1;
            ptLast.Y = -1;
            ptOriginal.X = -1;
            ptOriginal.Y = -1;
            //e.consume();
            if (action)
            {
                xe = e.X;
                ye = e.Y;
                if (xs > xe)
                {
                    z = xs;
                    xs = xe;
                    xe = z;
                }
                if (ys > ye)
                {
                    z = ys;
                    ys = ye;
                    ye = z;
                }
                w = (xe - xs);
                z = (ye - ys);
                if ((w < 2) && (z < 2)) initvalues();
                else
                {
                    if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
                    else xe = (int)((float)xs + (float)z * xy);
                    xende = xstart + xzoom * (double)xe;
                    yende = ystart + yzoom * (double)ye;
                    xstart += xzoom * (double)xs;
                    ystart += yzoom * (double)ys;
                }
                xzoom = (xende - xstart) /(double)x1;
                yzoom = (yende - ystart) / (double)y1;
                mandelbrot();
                rectangle = false;
                //Refresh();
            }
        }
        else if (checkBox1.Checked == true)
        {
            int z, w;
            this.Cursor = Cursors.Arrow;
            // Set internal flag to know we no longer "have the mouse".
            weHaveMouse = false;
            // If we have drawn previously, draw again in that spot
            // to remove the lines.
            if (ptLast.X != -1)
            {
                toolStripStatusLabel1.Text = "Please click and drag to zoom into the Fractal";
                Point ptCurrent = new Point(e.X, e.Y);
                MyDrawReversibleRectangle(ptOriginal, ptLast);
            }
            // Set flags to know that there is no "previous" line to reverse.
            ptLast.X = -1;
            ptLast.Y = -1;
            ptOriginal.X = -1;
            ptOriginal.Y = -1;
            //e.consume();
            if (action)
            {
                xe = e.X;
                ye = e.Y;
                if (xs > xe)
                {
                    z = xs;
                    xs = xe;
                    xe = z;
                }
                if (ys > ye)
                {
                    z = ys;
                    ys = ye;
                    ye = z;
                }
                w = (xe - xs);
                z = (ye - ys);
                if ((w < 2) && (z < 2)) initvalues();
                else
                {
                    if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy);
                    else xe = (int)((float)xs + (float)z * xy);
                    xende = xstart + xzoom * (double)xe;
                    yende = ystart + yzoom * (double)ye;
                    xstart += xzoom * (double)xs;
                    ystart += yzoom * (double)ys;
                }
                xzoom = (xende - xstart) / (double)x1;
                yzoom = (yende - ystart) / (double)y1;
                mandelbrot();
                rectangle = false;
                //Refresh();
            }
        }

    }
4

1 回答 1

0

首先,以我的拙见,缩小时没有必要画橡皮筋。只需单击缩小的图像居中的位置。尽管如此,我看不出您提供的放大和缩小代码之间的代码差异。一般来说,最简单的做法是缩小图像,使图像的当前中心保持在其位置,并以 X 和 Y 坐标除以 2 和 Width 和 Height 相乘的方式处理图像绘制2.如果是您的代码,在代码末尾计算类似这样的东西可能会奏效:

xzoom = 1/xzoom;
yzoom = 1/yzoom;
于 2012-10-17T14:19:01.450 回答