基本的に私がやろうとしているのは、マウスイベントを使用して画像を回転させることです。たとえば、マウスの左ボタンを押したままマウスを上下に動かすと、画像が回転します。私はここで別の質問を見つけました ( 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);
}
}
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;
}
ゲイブと他のみんなに感謝します!