基本上我想做的是使用鼠标事件旋转图片。例如,按住鼠标左键时,上下移动鼠标时图片会旋转。我在这里发现了另一个问题,几乎和我的一样(如何在 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 和其他所有人!