【问题标题】:Rotated text align in C#C#中的旋转文本对齐
【发布时间】:2010-12-16 11:25:44
【问题描述】:

我需要能够旋转标签中的文本并将其左对齐、右对齐或居中对齐。到目前为止,我可以在派生标签的 onPaint 方法中使用此代码进行旋转:

 float width = graphics.MeasureString(Text, this.Font).Width;
 float height = graphics.MeasureString(Text, this.Font).Height;

 double angle = (_rotationAngle / 180) * Math.PI;
 graphics.TranslateTransform(
     (ClientRectangle.Width + (float)(height * Math.Sin(angle)) - (float)(width * Math.Cos(angle))) / 2,
     (ClientRectangle.Height - (float)(height * Math.Cos(angle)) - (float)(width * Math.Sin(angle))) / 2);
 graphics.RotateTransform(270f);
 graphics.DrawString(Text, this.Font, textBrush, new PointF(0,0), stringFormat);
 graphics.ResetTransform();

而且效果很好。我可以看到文字旋转了 270 度。

但是当我尝试在 stringFormat 中设置对齐时,它变得疯狂,我无法弄清楚发生了什么。

如何让文本旋转 270 度并向上对齐?

【问题讨论】:

  • 你在设置什么对齐方式?
  • 一开始是Near,后来想换成Far和Center
  • 当你变换图形时,整个“世界”都会被变换,所以近处与近处并不完全相同。不能设置在你想要的位置吗?

标签: c# rotation drawstring


【解决方案1】:

如果有人在寻找提示,这里是 0、90、180、270 和 360 度旋转的解决方案,其中 StringAligment 可以工作。

一件事是选择正确的点将原点移动到,第二件事是根据旋转修改显示矩形。

StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;

SizeF txt = e.Graphics.MeasureString(Text, this.Font);
SizeF sz = e.Graphics.VisibleClipBounds.Size;

//90 degrees
e.Graphics.TranslateTransform(sz.Width, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();

//180 degrees
e.Graphics.TranslateTransform(sz.Width, sz.Height);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();

//270 degrees
e.Graphics.TranslateTransform(0, sz.Height);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();

//0 = 360 degrees
e.Graphics.TranslateTransform(0, 0);
e.Graphics.RotateTransform(0);
e.Graphics.DrawString(Text, this.Font, Brushes.Black, new RectangleF(0, 0, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();

如果您将此代码放在标签的 OnPaint 事件中,它将显示四次旋转表单的标题。

【讨论】:

  • 这可行,但质量比 Windows 10 上的普通 Forms.Label 差。GDI+ 的绘图质量令人遗憾。我必须使用 DrawTextW() API 将绘图实现为位图,并使用 RotateFlip() 旋转该位图以获得高质量!
【解决方案2】:

如果您需要在非 0 X,Y 处绘制,请扩展 Adrian Serafin 的答案:

//90 degrees
e.Graphics.TranslateTransform(sz.Width, 0);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format);
e.Graphics.ResetTransform();
//180 degrees
e.Graphics.TranslateTransform(sz.Width, sz.Height);
e.Graphics.RotateTransform(180 this.Font, Brushes.Black,
  new RectangleF(-sz.ToPointF().X, -sz.ToPointF().Y, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();
//270 degrees
e.Graphics.TranslateTransform(0, sz.Height);
e.Graphics.RotateTransform(270);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(-sz.ToPointF().Y, sz.ToPointF().X, sz.Height, sz.Width), format);
//0 = 360 degrees
e.Graphics.TranslateTransform(0, 0);
e.Graphics.RotateTransform(0);
e.Graphics.DrawString(Text, this.Font, Brushes.Black,
  new RectangleF(sz.ToPointF().X, sz.ToPointF().Y, sz.Width, sz.Height), format);
e.Graphics.ResetTransform();

【讨论】:

  • 我们可以用它来绘制自定义角度的字符串,如 45、60、12 吗?
  • @Adhi 这是三角函数。 Sin 90=1,Cos 90=0。正弦 180=-1,余弦 180=-1。我想一个更可靠的答案可能是使用数学库根据角度动态计算 x,y 坐标。我不再有可以用于测试的应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 2011-08-25
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
相关资源
最近更新 更多