在我的C#程序中,我使用RotateTransform方法旋转我想要绘制的图片。这已经起作用了,但我找不到如何改变图片旋转的中心点。默认情况下,它是我的图片框的左下角,不幸的是,我需要在(760,480)像素上围绕另一个点旋转。
我到处都找过了,只找到了这个CenterX属性。CenterX msdn
无论如何,我似乎没有在Visual Studio中找到这个属性,所以我想我这样做是错误的。
我当前的代码如下所示:
*e.Graphics.RotateTransform(angle);
e.Graphics.DrawLine(Pens.Black, physicObj.lineStartingPoint, physicObj.lineEndingPoint);
e.Graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(physicObj.leftCornerCircle, physicObj.circleSize));
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(physicObj.leftCornerRectangle, physicObj.rectangleSize));*此零件工作正常,但使用了错误的中心点进行旋转。我试着用
e.Graphics.RotateTransform.CenterX = ... ;但是在e.Graphics.RotateTransform中似乎没有可访问的CenterX。Visual Studio在RotateTransform下面显示一条红线,说明它是一个方法,这在给定的上下文中是无效的。我不知道设置这个属性的方法,也没有找到任何这样做的编码示例,根据微软提供的信息(在链接中),我认为这是一种方法。
希望有人能解释一下我需要做些什么来改变这个中心点。谢谢!
发布于 2016-03-20 03:00:29
这很简单:
转换到center
的
float centerX = 760;
float centerY = 480;
e.Graphics.TranslateTransform(-centerX, -centerY);
e.Graphics.RotateTransform(angle);
e.Graphics.TranslateTransform(centerX, centerY);本质上,您创建3个矩阵并将它们相乘以获得结果-单个变换矩阵,这是2D和3D变换的基础。
另外,为了方便起见,您可以创建一个扩展方法:
public static class GraphicsExtensions
{
public static void TranslateTransform(this Graphics g, float x, float y, float angle)
{
g.TranslateTransform(-x, -y);
g.RotateTransform(angle);
g.TranslateTransform(x, y);
}
}https://stackoverflow.com/questions/36103705
复制相似问题