使用鼠标在C#中旋转图片

问题描述 投票:0回答:4

基本上,我想做的是使用鼠标事件来旋转图片。例如,在按住鼠标左键的同时,上下移动鼠标时图片旋转。我在这里发现了另一个问题,几乎和我的(How do I rotate a picture in C#)一样,但是当将rotation方法(链接中的方法源代码)中的angle参数映射到鼠标与图像中心之间的计算角度时,抛出了溢出异常。我要旋转的图像在图片框中。有任何想法吗?我应该以其他方式这样做吗?

提前感谢!

---------编辑1 -----------

好吧,我认为我的触发已关闭,我将其更改为...

角度= 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);
        }


    }

双击pictureBox时,rotateMode标志设置为true。谢谢大家!

---------答案-----------

感谢Gabe,我在代码中找到了所有的小缺陷,现在可以正常工作了。唯一的事情是我必须增大图片框的大小以适合旋转的图像。对于每个想知道答案的人来说,这是正确的代码。

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

感谢Gabe和其他所有人!

c# rotation image image-rotation mouse-coordinates
4个回答
1
投票

您是否尝试过类似this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);的方法?链接到的RotateImage函数返回了新的旋转图像,而不是如代码所愿地就位旋转。

不幸的是,这样做会使已经旋转的图像旋转。您需要做的是将原始图像存储在某个位置,例如originalImage。然后有this.pictureBox1.Image = RotateImage(originalImage, angle);。这样,它将始终旋转原始文件的新版本。


1
投票

没有看到您的代码,我们只能猜测。我的猜测是您正在进行某种触发计算,例如:

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

如果您的x2-x1趋于0,则您对Math.Atan的论点将趋于无穷大并溢出。


0
投票

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

您在此处两次使用了imageCenterY,(尽管在您发布的代码中还可以)。>>

无论如何,我建议使用Atan2而不是Atan。


0
投票
     int  y2 = _cursory; // Cursor.Postion.Y

     int   y1 = (this.pictureBox1.Location.Y + (this.pictureBox1.Height / 2));
     int x2 = _cursorx; // Cursor.Postion.X
     int   x1 = (this.pictureBox1.Location.X + (this.pictureBox1.Width / 2));

      float  angle = (float)Math.Atan2((y1 - y2), (x1 - x2));
      float xDistance = x1 - this.pictureBox1.Location.X;
      float yDistance = y1 - this.pictureBox1.Location.Y;

      double deg = angle / Math.PI * 180 + 180;
      pictureBox1.Image = RotateImage(img, (int)deg);
© www.soinside.com 2019 - 2024. All rights reserved.