C#-(绘图列-右上)drawString问题

时间:2018-11-07 15:04:19

标签: c#

如您在图像中看到的,这就是结果。我想在绘制线条的右上角绘制drawString

https://i.stack.imgur.com/clxbA.png

    public void Form_Outline()
    {

        // top left A 
        // top right B
        // bottom left C
        // bottom right D

        WriteLog("Start");

        WriteLog(string.Format("Height {0}",Tile_Grid.Height));
        WriteLog(string.Format("Width {0}", Tile_Grid.Width));

        Graphics gDraw = Tile_Grid.CreateGraphics();

        Pen Black_Pen = new Pen(Color.Black, 5);
        Pen Red_Pen = new Pen(Color.Red, 1);

        gDraw.DrawRectangle(Black_Pen, 0, 0, Tile_Grid.Width, Tile_Grid.Height);


        int Column_Quanity = Int32.Parse(Vertical_Lines.Text);

        var Column_Seperation_Size = Tile_Grid.Width / Column_Quanity;
        var Column_Count = 0;

        for (int i = 0; i < Column_Quanity; i++)
        {
            Column_Count += Column_Seperation_Size;

            WriteLog(i.ToString());

            if (!(i == (Column_Quanity - 1))) { 
                gDraw.DrawLine(Red_Pen, Column_Count, 0, Column_Count, Tile_Grid.Height);
            }
            gDraw.DrawString((i+1).ToString(), new Font("Arial", Column_Seperation_Size / 5), new SolidBrush(Color.Black), Column_Count, 0, new StringFormat());
        } 

        gDraw.Dispose();
        WriteLog("End");

    }

1 个答案:

答案 0 :(得分:0)

您需要测量字符串并将其从x位置减去以将数字移到上一列。

var number = (i+1).ToString();
var emSize = Column_Seperation_Size / 5;
var font = new Font("Arial", emSize)
var size = gDraw.MeasureString(number, font);
var brush = new SolidBrush(Color.Black);
g.DrawString(number, font, brush, Column_Count - size.Width, 0);