通过反转放大鼠标缩小

时间:2012-10-17 14:06:02

标签: c# winforms bitmap zoom mouseevent

我有一个代码块,它在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();
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

首先,根据我的拙见,当您缩小时不需要绘制橡皮筋。只需单击缩小图像将居中的位置。然而,我无法看到您在放大和缩小代码之间提供的代码差异。一般来说,最简单的方法是缩小图像,使图像的当前中心保持在其位置,并以X和Y坐标除以2,宽度和高度相乘的方式处理图像绘制2。 如果是你的代码,在代码结束时计算这样的东西可能可以做到这一点:

xzoom = 1/xzoom;
yzoom = 1/yzoom;