C#中使用鼠标位置旋转图片
基本上我想做的是使用鼠标事件旋转图片。例如,按住鼠标左键时,上下移动鼠标时图片会旋转。我在这里发现了另一个问题,几乎和我的一样(如何旋转图片在 C# 中),但是当将旋转方法(链接中的方法源代码)中的角度参数映射到鼠标与图像中心之间的计算角度时,我抛出了溢出异常。我尝试旋转的图像位于图片框中。有什么想法吗?我应该以另一种方式做这件事吗?
提前致谢!
---------编辑 1-----------
好吧,我认为我的三角关闭了,我将其更改为...
Angle = 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);
}
}
。双击图片框。
---------答案------------
感谢加布,我发现了我的代码中的所有小问题,现在它工作正常。问题是我必须增大图片框的尺寸以适应旋转图像。这是为每个想知道答案的人提供的正确代码,
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;
}
谢谢加布和其他人!
Basically what I'm trying to do is have a picture rotate using mouse events. For example, while you hold down the left mouse button, the picture rotates when you move the mouse up and down. I found another question on here almost like mine (How do I rotate a picture in C#) but when mapping the angle parameter in the rotate method (method source code in link) to the calculated angle between the mouse and the center of the image I get an overflow exception thrown. The image I'm trying to rotate is in a picture box. Any ideas? Should I be doing this another way?
Thanks in advance!
---------EDIT 1-----------
Ok I think my trig was off, I changed it to...
Angle = Atan((mousePosY - imageCenterY)/(mousePosX - imageCenterX)
But now the image doesn't rotate, it just moves (I programmed the ability for it to move as well but that works fine). Here's the piece of code I'm dealing with.
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);
}
}
The rotateMode flag is set to true when the pictureBox is double clicked. Thanks all!
---------ANSWER-----------
Thanks to Gabe I found all of the little kinks in my code and it works fine now. Only thing is I had to make the size of the picture box larger to fit the rotated image. Here is the correct code for everyone who wants to know the 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;
}
Thanks Gabe and everyone else!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过类似
this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);
的方法?您链接的RotateImage
函数返回了一个新的旋转图像,而不是像您的代码期望的那样就地旋转它。不幸的是,这样做会旋转已经旋转的图像。您需要做的是将原始图像存储在某个地方,例如
originalImage
。然后有this.pictureBox1.Image = RotateImage(originalImage, angle);
。这样它总是会轮换原始版本的新版本。Have you tried something along the lines of
this.pictureBox1.Image = RotateImage(this.pictureBox1.Image, angle);
? TheRotateImage
function you linked to returned a new rotated image rather than rotating it in-place as your code expects.Unfortunately, doing that rotates an already-rotated image. What you need to do is have the original image stored somewhere, say
originalImage
. Then havethis.pictureBox1.Image = RotateImage(originalImage, angle);
. That way it always rotates a fresh version of the original.如果没有看到您的代码,我们只能猜测。
我的猜测是,您正在进行某种三角计算,例如:
如果您的 x2-x1 趋向于 0,则您对 Math.Atan 的参数趋于无穷大,并溢出。
Without seeing your code, we can only guess.
My guess is that you're doing some kind of trig calculation, such as:
If your x2-x1 goes towards 0, your argument to Math.Atan goes towards infinity, and overflows.
您在那里使用
imageCenterY
两次,(尽管在您发布的代码中没问题)。无论如何,我建议使用 Atan2 而不是 Atan。
You are using
imageCenterY
twice there, (although its ok in the code you posted).Anyway I would suggest using Atan2 rather than Atan.
// img == 原始图像
// img == original image