C#旋转变换

zuh*_*h4n 6 .net c# winforms

我可以旋转面板和文字90º,它适合我.但旋转180º不起作用,我看不到文字.我该怎么办才能修复它?

else if (m_orientation == AfyLabelOrientation.TurnedLeft90)
        {
            e.Graphics.TranslateTransform(0, this.Height - 5);
            e.Graphics.RotateTransform(270);

            if (!TextShadow_)
            {
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
            }
            else if (TextShadow_)
            {
                //Drawing text shadow
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Gray), new RectangleF(Padding.Left + 1, Padding.Top - 1, this.Height, this.Width));

                //Drawing text
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
            }
        }
        else if(m_orientation == AfyLabelOrientation.Overturned)//This don't work
        {
            e.Graphics.TranslateTransform(this.Width, 0);
            e.Graphics.RotateTransform(180);

            if (!TextShadow_)
            {
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
            }
            else if (TextShadow_)
            {
                //text shadow
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Gray), new RectangleF(Padding.Left + 1, Padding.Top - 1, this.Height, this.Width));

                //text
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(Padding.Left, Padding.Top, this.Height, this.Width));
            }
        }
Run Code Online (Sandbox Code Playgroud)

But*_*zke 7

如果我得到它,你需要转换为对象以保持其中心.

RotateTransform始终围绕原点旋转.因此,您需要先将旋转中心转换为原点,然后旋转,然后将其平移.

//move rotation point to center of image
g.TranslateTransform((float)this.Width/2, (float)this.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)this.Width/2,-(float)this.Height / 2);
Run Code Online (Sandbox Code Playgroud)