C# 旋转转换

本文关键字:转换 旋转 | 更新日期: 2023-09-27 18:33:39

我可以将面板和文本旋转 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));
            }
        }

C# 旋转转换

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

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);

可能是您尝试旋转的内容位于容器的左上角。然后,旋转将围绕对象的左上角旋转,因此 180 度旋转会将对象移动到视图窗口之外。

________
|text   |
_________

旋转成类似这样的东西:

    _______
text|      |
    ________

当然,我不是在画text旋转,而只是试图指示它的位置。将旋转点移动到文本中间,或在旋转后按文本宽度向右移动文本,以将文本放在正确的位置结束。