如何在C#中打印垂直文本? (StringFormat.DirectionVirtical打印下来)

问题描述:

我想使用System.Drawing在C#中打印文本,但设置StringFormat.DirectionVirtical标志似乎只向下打印文本。我希望它以另一种方式打印,就像你在图表中看到的一样。如何在C#中打印垂直文本? (StringFormat.DirectionVirtical打印下来)

这将不仅仅是形式,所以我想看看是否有办法在绘图时不使用变换矩阵来做到这一点。

有什么办法可以做到这一点?

使用Graphics.RotateTransform使文本以您想要的方式旋转。例如:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
    } 
    protected override void OnPaint(PaintEventArgs e) { 
     string text = "Vertical text"; 
     SizeF textSize = e.Graphics.MeasureString(text, this.Font); 
     e.Graphics.RotateTransform(-90); 
     e.Graphics.DrawString(text, this.Font, Brushes.Black, 
      new PointF(-textSize.Width, 0)); 
    } 
}