使用矩阵转换(旋转)图像 - 设计时与运行时渲染结果
我有一个使用变换矩阵旋转的 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。
I have a 48x48 image which is rotated using a transformation matrix.
For some reason, the rotated image in design-time differs from the rotated image in run-time as you can see from this screenshot (link dead) (design-time on the left, run-time on the right):
It might be a little bit difficult to spot, but if you look closely at the right edge of the blue circle, it is about a pixel wider in the image to the right. Note that the image is layered - the white glow in the foreground is the part that's being rotated, while the blue ball in the background is static.
It seems like the image is offset 1 pixel in run-time, when rotating exactly 90 degrees (as in the screenshot), 180 degrees and probably also 270 degrees. The image looks the same with any other rotation angle, as far as I can see.
Here's a snippet:
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)
}
Can anyone explain the reason for this behaviour? Or better yet, a solution to make run-time results look like design-time results?
It's important for me because this image is part of an animation which is generated from code based on a single image which is then rotated in small steps. In design-time, the animation looks smooth and nice. In run-time it looks like it's jumping around :/
I'm using Visual Studio 2005 on Windows Vista Business SP2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能与设计时使用的 Graphics 对象与运行时使用的 Graphics 对象(如 PixelOffsetMode)的差异有关。
It might have something to do with differences in the Graphics object used at design time versus run time like the PixelOffsetMode.
您如何指定旋转角度? 以度为单位还是以弧度为单位?
有时,当我不小心以度为单位指定角度时,我会得到像你所描述的结果,而它应该是弧度(结果对象旋转了几百度而不是几度)
我不确定给出你的具体例子,但值得建议。
How are you specifying your angle of rotation? in Degrees or in Radians?
Sometimes I get results like those you describe when I've accidentally specified my angle in degrees when it should've been radians (and as a result the object has been rotated several hundred degrees instead of a few)
I don't know for sure given your specific example, but its worth suggesting.
嗯,我的考虑是尝试:
Hm, my consideration is to try:
我认为这应该是关于以浮点执行数学的问题; 当四舍五入到整数坐标时。
一个可能相关的问题......您的图像是否完全正方形且宽度和高度为偶数? 也许问题就出在这里。
编辑:在第一次阅读时,我通过了图像的尺寸...如果这不是问题,也许你可以自己做一个简单的旋转:(
我认为索引交换没问题,但双重检查不会坏)
I think that should be a problem about the math being performed in floating point; when rounding back to integer coordinates.
One question that might be relevant... is your image completely square and the width and height an even number? Maybe the problem lies there.
Edit: In the first read I passed through the dimensions of the image... If that wasn't the problem maybe you could do a simple rotation by yourself:
(I think the index swappings are ok, but a double check won't be bad)