在不混合图像的情况下定时旋转图像



我正在尝试在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);
    }

轮子的图像继续旋转,颜色混合,然后图像只是淡出。我该如何解决这个问题?

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

解决方案是获取原始图像的副本,然后每次恢复原始副本并以新的角度旋转。这样,您将始终获得单个旋转完成,并且您不会累积扭曲。

最新更新