基本上我想要做的是有使用鼠标事件的图片旋转。例如,当您按住鼠标左键,图片旋转,当你移动鼠标上下。我发现了另一个问题就在这里几乎就像矿(如何旋转图片在C#),但映射在旋转法(角度参数在链接方法源代码)鼠标和图像的中心之间的计算出的角度,当我得到抛出溢出异常。我试图旋转图像是在一个图片框。有任何想法吗?我应该这样做另一种方式?

提前感谢!

--------- EDIT 1 -----------

好吧,我想我的TRIG被关闭,我改成了......

角度= ATAN((mousePosY - imageCenterY)/(mousePosX - imageCenterX)

但现在的图像不旋转,它只是移动(我编程为它以及移动的能力,但工作正常)。这里是一片我处理代码。

    private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;

        pbCurrentX = e.X;
        pbCurrentY = e.Y;

    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        // For moving the image
        if (isDragging)
        {
            this.pictureBox1.Top = this.pictureBox1.Top + (e.Y - pbCurrentY);
            this.pictureBox1.Left = this.pictureBox1.Left + (e.X - pbCurrentX);
        }

        // For rotating the image
        if (rotateMode  && isDragging)
        {
            y2 = e.Y;
            y1 = (this.pictureBox1.Location.Y + (this.pictureBox1.Height / 2));
            x2 = e.X;
            x1 = (this.pictureBox1.Location.X + (this.pictureBox1.Width / 2));

            angle = (float)Math.Atan((y2-y1)/(x2-x1));

            // RotateImage method from the other question linked above
            this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);
        }


    }

在rotateMode标志被设置为true时在PictureBox被双击。谢谢大家!

--------- ANSWER -----------

感谢加布,我发现在我的代码的小扭结,现在工作得很好。唯一的一点是,我不得不使画面的大小框较大,以适应旋转图像。下面是为大家谁想要知道的答案正确的代码。

private void pictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;

        pbCurrentX = e.X;
        pbCurrentY = e.Y;

    }

    private void pictureBox_MouseMove(object sender, MouseEventArgs e)
    {

        if (isDragging && !rotateMode)
        {
            this.pictureBox1.Top = this.pictureBox1.Top + (e.Y - pbCurrentY);
            this.pictureBox1.Left = this.pictureBox1.Left + (e.X - pbCurrentX);
        }

        if (rotateMode  && isDragging)
        {
            y2 = e.Y;
            y1 = (this.pictureBox1.Location.Y + (this.pictureBox1.Height / 2));
            x2 = e.X;
            x1 = (this.pictureBox1.Location.X + (this.pictureBox1.Width / 2));

            angle = (float)Math.Atan2((y1 - y2), (x1 - x2));

            pictureBox1.Image =  RotateImage(currentImage, (100*angle));
        } 

    }

    private void pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

感谢加布和其他人!

有帮助吗?

解决方案

你试过沿this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);线的东西?您链接到返回的新旋转的图像,而不是就地为您的代码需要旋转它的RotateImage功能。

不幸的是,这样做旋转的已旋转的图象。你需要做的是有地方存储原始图像的,说originalImage。然后有this.pictureBox1.Image = RotateImage(originalImage, angle);。这样,它总是旋转原来的最新版本。

其他提示

没有看到你的代码,我们只能猜测。 我的猜测是,你正在做某种三角函数的计算,如:

theta = Math.Atan((y2 - y1) / (x2 - x1));

如果您的X2-X1伴随朝向0,您的参数Math.Atan伴随朝向无穷大,并溢流。

  

角度= ATAN((mousePosY - imageCenterY)/(mousePosX - imageCenterY)

您正在使用imageCenterY有两次,(虽然它在您发布的代码确定)。

反正我建议使用 ATAN2 而比ATAN。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top