编写包含某些图形的WinForms应用。要旋转图像,请使用e.Graphics.RotateTransform(angle)方法,然后绘制图像。几行后,我调用e.Graphics.RotateTransform(0),这样我旋转的其他图像就不会出现,应该看起来是正常的。但是,情况并非如此,看来我以后绘制的所有内容都受相同的Transforms的影响。为什么RotateTransform(0)不能将页面的“转换”重置为正常状态?
注意:这是我的代码中_Paint事件产生的作用,但这是问题所在。我尝试包括任何可能相关的内容
e.Graphics.TranslateTransform(Chute1Rect.Width / 2 + Chute1Rect.X, Chute1Rect.Height / 2 + Chute1Rect.Y);
if (increasing) //Chute is rotating out of the way
{
e.Graphics.RotateTransform(chuteAngle);
e.Graphics.TranslateTransform(-1 * (Chute1Rect.Width / 2 + Chute1Rect.X), -1 * (Chute1Rect.Height / 2 + Chute1Rect.Y));
e.Graphics.DrawImage(ChuteRotating, Chute1Rect);
//These statements do not seem to be resetting the graphics drawing by the time we do the trains
e.Graphics.TranslateTransform(Chute1Rect.Width / 2 + Chute1Rect.X, Chute1Rect.Height / 2 + Chute1Rect.Y);
e.Graphics.RotateTransform(0);
}
else //Chute is rotating into load position
{
if (chuteAngle == 0) //Chute has rotated into load position
{
e.Graphics.TranslateTransform(-1 * (Chute1Rect.Width / 2 + Chute1Rect.X), -1 * (Chute1Rect.Height / 2 + Chute1Rect.Y));
//Getting here means that the chute can be lifted & lowered because we are rotated down
if (lifting) //Chute is lifting out of the way
{
Chute1SlideRect.Y -= chuteDistance;
Chute2SlideRect.Y -= chuteDistance;
e.Graphics.DrawImage(ChuteSlide, Chute1SlideRect);
e.Graphics.DrawImage(ChuteSlide, Chute2SlideRect);
}
else //Chute is lifting into load position
{
Chute1SlideRect.Y -= chuteDistance;
Chute2SlideRect.Y -= chuteDistance;
e.Graphics.DrawImage(ChuteSlide, Chute1SlideRect);
e.Graphics.DrawImage(ChuteSlide, Chute2SlideRect);
}
e.Graphics.DrawImage(ChuteJoint, Chute1JointRect);
e.Graphics.DrawImage(ChuteJoint, Chute2JointRect);
}
else
{
e.Graphics.RotateTransform(chuteAngle);
e.Graphics.TranslateTransform(-1 * (Chute1Rect.Width / 2 + Chute1Rect.X), -1 * (Chute1Rect.Height / 2 + Chute1Rect.Y));
e.Graphics.DrawImage(ChuteRotating, Chute1Rect);
//These statements do not seem to be resetting the graphics drawing by the time we do the trains
e.Graphics.TranslateTransform(Chute1Rect.Width / 2 + Chute1Rect.X, Chute1Rect.Height / 2 + Chute1Rect.Y);
e.Graphics.RotateTransform(0);
}
}
//Down below here is where the trains get drawn
最佳答案
MSDN docs对此没有明确说明,但似乎RotateTransform
总是修改现有的转换矩阵,在这种情况下,RotateTransform(0)
表示“不执行任何操作”,而不是“将旋转设为0”。尝试将其旋转到初始角度的相反方向。
编辑:或使用ResetTransform()
(docs)使事情回到您开始的地方并从那里开始。