3

我有一个使用变换矩阵旋转的 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。

4

4 回答 4

3

它可能与设计时使用的 Graphics 对象与运行时(如 PixelOffsetMode)的差异有关。

于 2009-07-14T11:35:08.923 回答
0

你如何指定你的旋转角度?度数还是弧度?

有时我会得到像你描述的那样的结果,当我不小心指定我的角度时,它应该是弧度(结果对象已经旋转了几百度而不是几度)

鉴于您的具体示例,我不确定,但值得建议。

于 2009-07-14T09:16:43.913 回答
0

嗯,我的考虑是尝试:

lMatrix.RotateAt(pAngle, new PointF((pImage.Width + 1)/2.0f, (pImage.Height + 1)/ 2.0f));
于 2009-07-14T09:37:00.490 回答
0

我认为这应该是浮点运算的问题;当舍入到整数坐标时。

一个可能相关的问题......你的图像是完全正方形的,宽度和高度是偶数吗?也许问题出在那儿。

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

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]

(我认为索引交换还可以,但仔细检查也不错)

于 2009-07-14T13:29:59.563 回答