如何根据旋转缩放图像

时间:2013-04-07 03:32:14

标签: c# image xna rotation scale

我想缩放图像,使图像始终与屏幕的大小无关。有没有人对此事有任何想法?顺便说一句,我在C#中用xna编程。

2 个答案:

答案 0 :(得分:0)

您可以应用RotateTransform转换图像,然后将其包含在LayoutTransform中以填充容器的尺寸(在本例中为屏幕)。

答案 1 :(得分:0)

也许是类似的东西,虽然我不确定你期望如何绘制纹理。使用三角形和纹理包装它们将是最简单的。

这是旋转后获得新宽度和新高度的方式:

Matrix origin = Matrix.CreateTranslation(0, 0, 0);
Matrix scale = Matrix.CreateScale(1f);
Matrix rotation = Matrix.CreateRotationZ(MathHelper.ToRadians(rotate));
Matrix translation = Matrix.CreateTranslation(0, 0, 0);

Vector2 pos1 = Vector2.Transform(new Vector2(Texture.Width / 2, Texture.Height / 2), origin * scale * rotation * origin);
Vector2 pos2 = Vector2.Transform(new Vector2(Texture.Width, Texture.Height), origin * scale * rotation * translation);

int width = (int)Math.Abs(pos2.X - pos1.X) * 2;
int height = (int)Math.Abs(pos2.Y - pos1.Y) * 2;

float scaleX = (graphics.PreferredBackBufferWidth / width);
float scaleY = (graphics.PreferredBackBufferHeight / height);

您可能会想出最好的绘制方法,因为翻转45度的图像在屏幕上看起来很奇怪,因此您可能需要将其缩放以使其适合屏幕但仍然可以旋转。你遗漏了,旋转180度或90度的图像应该会更好。