我想要的是移动物体并沿其中心点旋转。我已经使用Matrix类进行转换:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.ResetTransform();
Matrix transformationMatrix = new Matrix();
transformationMatrix.RotateAt(rot, new PointF(img.Size.Width / 2, img.Size.Height / 2));
e.Graphics.Transform = transformationMatrix;
e.Graphics.DrawImage(img, 0, 0, img.Size.Width, img.Size.Height);
}
以上代码将沿中心旋转图像。
但如果我尝试移动它(我将图像放在pictureBox的中心),图像就不再沿着它的中心点旋转。
e.Graphics.DrawImage(img, (pictureBox1.Width - img.Size.Width) / 2, (pictureBox1.Height - img.Size.Height) / 2, img.Size.Width, img.Size.Height);
现在我想我必须使用Translate函数来指定位置,但我不知道如何做到这一点。翻译需要相对位置。我想使用其中心点指定图像位置,并能够沿其中心旋转它。
更新2:
修改后的代码看起来像这样
origin.X = 50;
origin.Y = 50;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TranslateTransform(origin.X, origin.Y);
e.Graphics.RotateTransform(rot);
e.Graphics.DrawImage(img, -img.Size.Width, -img.Size.Height/2, img.Size.Width, img.Size.Height);
}
所以我定义了Point origin来指定我的图像的位置。但它仍然不会沿着它的中心旋转。
答案 0 :(得分:1)
是的,您想使用翻译功能。以下是我为另一个问题撰写的示例,该问题展示了如何翻译和旋转图像:
https://stackoverflow.com/a/10956388/351385
更新
您要做的是将转换点设置为窗口中对象中心所在的点。这会导致显示的[0, 0]
点成为该点,因此任何旋转都会围绕它发生。然后在绘制图像时,使用图像[image width / 2, image height / 2]
的中点作为DrawImage方法的坐标。
再次更新
对不起,传递给DrawImage的坐标是图像[0 - width / 2, 0 - height / 2]
的否定中点。