Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Rotate Transform

Tags:

c#

.net

winforms

I can rotate the Panel and Text 90º and it works for me. But rotating 180º doesn't work, I can't see the text. What can I do to fix it?

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));
            }
        }
like image 795
zuh4n Avatar asked Oct 03 '13 11:10

zuh4n


2 Answers

If I got it, you need to translate to the object to maintain its center.

RotateTransform always rotates about the origin. So you need to first translate your centre of rotation to the origin, then rotate, then translate it back.

//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);
like image 55
Butzke Avatar answered Nov 13 '22 21:11

Butzke


It may be that what you are trying to rotate is in the left top corner of container. Then, rotation rotates around left top corner of your object, so 180 degrees rotation moves your object outside of view window.

________
|text   |
_________

is rotated into something like:

    _______
text|      |
    ________

of course I'm not painting text rotated, but just trying to idicate its position. Move rotation point to the middle of text or move text by its width to the right after rotation to end with the text being put in correct place.

like image 36
Jarek Avatar answered Nov 13 '22 22:11

Jarek