旋转文本进行打印

Pri*_*his 9 c# printing graphics

我正在使用PrintDocument打印页面.在某一点上,我想将文本旋转90度并打印它,即垂直打印文本.有任何想法吗 ???

g.RotateTransform(90);

不适用于OnPaint.

Fre*_*örk 27

当您调用RotateTransform时,您需要注意坐标系最终的位置.如果运行以下代码,则"倾斜文本"将显示在左边缘的左侧; 所以它不可见:

e.Graphics.Clear(SystemColors.Control);
e.Graphics.DrawString("Normal text", this.Font, SystemBrushes.ControlText, 10, 10);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString("Tilted text", this.Font, SystemBrushes.ControlText, 10, 10);
Run Code Online (Sandbox Code Playgroud)

由于您已将绘图表面倾斜90度(时钟方式),因此y坐标将沿右/左轴(从您的角度)而不是向上/向下移动.左边是更大的数字.因此,要将倾斜文本移动到曲面的可见部分,您需要减小y坐标:

e.Graphics.Clear(SystemColors.Control);
e.Graphics.DrawString("Normal text", this.Font, SystemBrushes.ControlText, 10, 10);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString("Tilted text", this.Font, SystemBrushes.ControlText, 10, -40);
Run Code Online (Sandbox Code Playgroud)

默认情况下,坐标系的原点位于曲面的左上角,因此RotateTransform将围绕该轴旋转曲面.

这是一张图片说明了这一点; 黑色在调用RotateTransform之前,红色在调用RotateTransform(35)之后:

图

  • 谢谢弗雷德里克。有效。我希望MSDN这样描述它。 (2认同)