在定时器上旋转图像而不混合图像

问题描述:

我试图制作一个在c#中使用定时器的*动画(pictureBox上的*图像)。旋转图像的在定时器上旋转图像而不混合图像

方法:

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; 
    } 

//为计时器滴答

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

代码轮的图像保持纺丝和颜色被混合,然后将图像刚刚淡出。 我该如何解决这个问题?

+0

什么是'anglePerSec'值,什么是'timer1.Interval'值?你有没有尝试增加间隔? – SergeyS

当图像旋转90度或90度的精确倍数的任何角度时,所有像素都会被保留,并且它们会移动到新的位置。但是,当以任何其他角度旋转时,会发生重新采样或近似,并且没有单个像素会移动到新的像素位置,因为像素位置是整数,但旋转角度会产生非整数位置。这意味着每个像素的新颜色将来自预旋转图像的4和6像素之间的混合。这种混合会导致你看到的褪色。结果,反复旋转会引起越来越多的失真,直到图像被显着改变或者甚至被完全破坏。

解决方案是拍摄原始图像的副本,然后每次恢复原始副本并以新角度旋转。这样你总是可以完成一次旋转,而且不会累积扭曲。