2016-09-17 11 views
2

C#でタイマーを使用してホイールの回転をアニメーション化しようとしています(画像ボックスのホイール画像)。画像を回転させる画像をブレンドせずにタイマーで画像を回転する

方法:タイマーティック

private void timer1_Tick(object sender, EventArgs e) 
    { 
     float anglePerTick = 0; 
     anglePerTick = anglePerSec/1000 * timer1.Interval; 
     pictureBox1.Image = RotateImage(pictureBox1.Image, anglePerTick); 
    } 

ホイールの画像のための

public static Image RotateImage(Image img, float rotationAngle) 
    { 
     //create an empty Bitmap image 
     Bitmap bmp = new Bitmap(img.Width, img.Height); 

     //turn the Bitmap into a Graphics object 
     Graphics gfx = Graphics.FromImage(bmp); 

     //now we set the rotation point to the center of our image 
     gfx.TranslateTransform((float)bmp.Width/2, (float)bmp.Height/2); 

     //now rotate the image 
     gfx.RotateTransform(rotationAngle); 

     gfx.TranslateTransform(-(float)bmp.Width/2, -(float)bmp.Height/2); 

     //set the InterpolationMode to HighQualityBicubic so to ensure a high 
     //quality image once it is transformed to the specified size 
     gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; 

     //now draw our new image onto the graphics object 
     gfx.DrawImage(img, new Point(0, 0)); 

     //dispose of our Graphics object 
     gfx.Dispose(); 

     //return the image 
     return bmp; 
    } 

//コード紡糸及び色がブレンドされ、その後、画像がちょうどフェードアウト保ちます。 これを修正するにはどうすればよいですか?

+0

「anglePerSec」という値は何ですか?「timer1.Interval」の値は何ですか?あなたはインターバルを上げようとしましたか? – SergeyS

答えて

1

画像を90度回転させたり、90度の倍数にすると、すべてのピクセルが保存され、新しい位置に移動します。しかし、他の角度で回転させると、リサンプリングや近似が行われ、ピクセル位置は整数ですが、そのような角度での回転は非整数の位置を生成するため、単一のピクセルは新しいピクセル位置に移動しません。これは、各ピクセルの新しい色が、事前回転されたイメージから4〜6ピクセルの間にブレンドされることを意味します。このブレンドはあなたが見る退色を引き起こすでしょう。その結果、イメージが大幅に変更されるか、または完全に破壊されるまで、反復回転はますます歪みを導入します。

解決策は、元のイメージのコピーを取り、その元のコピーを毎回復元し、新しい角度で回転させることです。この方法では、常に1回の回転が完了し、ゆがみが蓄積されることはありません。