我有一个使用变换矩阵旋转的 48x48 图像。

由于某种原因,设计时的旋转图像与运行时的旋转图像不同,您可以从 这个截图(链接失效) (左边是设计时,右边是运行时):

它可能有点难以发现,但如果仔细观察蓝色圆圈的右边缘,就会发现它在右侧图像中大约宽一个像素。请注意,图像是分层的 - 前景中的白色辉光是正在旋转的部分,而背景中的蓝色球是静态的。

当精确旋转 90 度(如屏幕截图所示)、180 度甚至可能 270 度时,图像在运行时似乎偏移了 1 个像素。据我所知,任何其他旋转角度的图像看起来都一样。

这是一个片段:

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。

有帮助吗?

解决方案

也许有些事情要与图形的差异在设计时对象使用与运行时间像PixelOffsetMode。

其他提示

你是如何规定的旋转角度?在度或弧度?

有时我喜欢那些时候我不小心把我指定的程度时,它应该已经被弧度角度你描述的结果(和作为结果的对象已旋转几百度,而不是几个)

我不知道肯定给你具体的例子,但它的价值建议。

嗯,我考虑的是尝试:

lMatrix.RotateAt(pAngle, new PointF((pImage.Width + 1)/2.0f, (pImage.Height + 1)/ 2.0f));

我认为应该是大约在浮点执行的数学问题;四舍五入时回整数坐标。

这可能是相关的一个问题... ...是你的形象完全正方形,宽度和高度为偶数?也许问题出在那里。

修改:在第一次读我通过图像的尺寸过去了......如果这不是问题,也许你可以自己做一个简单的旋转:

angle = 90 : img[i][j] = img[j][w-i]
angle = 180: img[i][j] = img[w-i][w-j]
angle = 270: img[i][j] = img[w-j][i]

(我认为索引swappings是好的,但双重检查不会差)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top