我有一个使用变换矩阵旋转的 48x48 图像。
出于某种原因,设计时的旋转图像与运行时的旋转图像不同,正如您从这个屏幕截图中看到的(链接失效)(左侧是设计时,右侧是运行时):
它可能有点难以发现,但如果你仔细观察蓝色圆圈的右边缘,它在右侧图像中大约宽了一个像素。请注意,图像是分层的 - 前景中的白光是正在旋转的部分,而背景中的蓝色球是静态的。
看起来图像在运行时偏移了 1 个像素,当正好旋转 90 度(如屏幕截图中所示)、180 度以及可能还有 270 度时。据我所知,图像在任何其他旋转角度下看起来都一样。
这是一个片段:
protected static Image RotateImage(Image pImage, Single pAngle)
{
Matrix lMatrix = new Matrix();
lMatrix.RotateAt(pAngle, new PointF(pImage.Width / 2, pImage.Height / 2));
Bitmap lNewBitmap = new Bitmap(pImage.Width, pImage.Height);
lNewBitmap.SetResolution(pImage.HorizontalResolution, pImage.VerticalResolution);
Graphics lGraphics = Graphics.FromImage(lNewBitmap);
lGraphics.Transform = lMatrix;
lGraphics.DrawImage(pImage, 0, 0);
lGraphics.Dispose();
lMatrix.Dispose();
return lNewBitmap;
}
void SomeMethod()
{
// Same results in design-time and run-time:
PictureBox1.Image = RotateImage(PictureBox2.Image, 18)
// Different results in design-time and run-time.
PictureBox1.Image = RotateImage(PictureBox2.Image, 90)
}
谁能解释这种行为的原因?或者更好的是,让运行时结果看起来像设计时结果的解决方案?
这对我来说很重要,因为这个图像是动画的一部分,它是由基于单个图像的代码生成的,然后以小步旋转。在设计时,动画看起来流畅而漂亮。在运行时它看起来像是在跳来跳去:/
我在 Windows Vista Business SP2 上使用 Visual Studio 2005。