How to rotate the list of rows along the axis?

I have a list of lines that I draw on the axis. I want to rotate the strings (again, they must remain on the same horizontal axis when turning). I am trying to use the following code:

namespace DrawString
{
    struct StringData
    {
        public string StringName;
        public int X;
        public int Y;
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Font mFont = new Font("Arial", 10.0f, FontStyle.Bold);
        List<StringData> data = new List<StringData> { new StringData() { StringName = "Label1", X = 10, Y = 30 },
                                                     new StringData() { StringName = "Label2", X = 130, Y = 30 }};

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (var str in data)
            {
                e.Graphics.RotateTransform(30);
                e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(str.X, str.Y));
                e.Graphics.ResetTransform();
            }
        }
    }
}

But it does not work, as the graphic rotates for both lines at the same time, causing one line above the other. How can I rotate them individually using the center of the line as my axis of rotation?

+3
source share
2 answers

It looks like you are describing something like this:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    foreach (var str in data)
    {
        e.Graphics.TranslateTransform(str.X, str.Y);
        e.Graphics.RotateTransform(30);
        e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(0, 0));
        e.Graphics.ResetTransform();
    }
}

, , . X, .

+1

        private void Form1_Paint(object sender, PaintEventArgs e) 
        { 
            e.Graphics.RotateTransform(30);
            foreach (var str in data) 
            { 
                e.Graphics.DrawString(str.StringName, mFont, new SolidBrush(Color.Black), new Point(str.X, str.Y)); 
            }
            e.Graphics.ResetTransform();  
        } 

0

Source: https://habr.com/ru/post/1764743/


All Articles