[英]Rotating a picture using mouse position in C#
基本上我想要做的是使用鼠标事件旋转图片。 例如,当您按住鼠标左键时,上下移动鼠标时图片会旋转。 我在这里发现了另一个几乎和我一样的问题( 如何在 C# 中旋转图片)但是当将旋转方法(链接中的方法源代码)中的角度参数映射到鼠标和图像中心之间的计算角度时,我得到一个溢出异常抛出。 我要旋转的图像在图片框中。 有任何想法吗? 我应该以另一种方式这样做吗?
提前致谢!
---------编辑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);
}
}
当图片框被双击时,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 和其他所有人!
没有看到你的代码,我们只能猜测。 我的猜测是您正在进行某种三角计算,例如:
theta = Math.Atan((y2 - y1) / (x2 - x1));
如果您的 x2-x1 趋于 0,则您对 Math.Atan 的参数趋于无穷大,并溢出。
你有没有尝试过this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);
? 您链接到的RotateImage
函数返回了一个新的旋转图像,而不是像您的代码期望的那样就地旋转它。
不幸的是,这样做会旋转已经旋转的图像。 您需要做的是将原始图像存储在某处,例如originalImage
。 然后有this.pictureBox1.Image = RotateImage(originalImage, angle);
. 这样它总是旋转原始版本的新版本。
角度 = Atan((mousePosY - imageCenterY)/(mousePosX - imageCenterY)
您在那里使用imageCenterY
两次,(尽管在您发布的代码中没问题)。
无论如何,我建议使用Atan2而不是 Atan。
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);
// img == 原始图片
这段代码对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.