C# Graphics DrawString VerticalDirection 从底部开始

发布于 2024-12-28 06:06:40 字数 476 浏览 0 评论 0原文

我需要在打印文档方法中使用 Graphics.DrawString 编写一个垂直方向的字符串,并且我对字符串宽度有限制,问题是字符串是从左到右编写的,我需要第一行在右边我正在使用下面的方法

SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1, StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

I need to write using Graphics.DrawString in a print Document method , a string with vertical direction and I have a limitation on the string width , the problem is that the string is writen from left to right and I need that the first line be on the right I'm using the method below

SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1, StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南街九尾狐 2025-01-04 06:06:40

正如 Hans 评论的那样,RotateTransform 可用于翻转字符串:

strF.Alignment = StringAlignment.Far;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

TranslateTransform 将坐标系的原点更改为 rec 矩形的右下角,然后 RotateTransform 翻转它180 度,然后将字符串的对齐方式更改为“远”,将字符串放置到原始字符串绘制的同一位置。

As Hans' commented, the RotateTransform can be used to flip the string around:

strF.Alignment = StringAlignment.Far;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

The TranslateTransform changes the origin of your coordinate system to the bottom right corner of your rec rectangle, then the RotateTransform flips it 180 degrees, and then the alignment of the string is changed to Far to place the string into the same place your original string was drawing.

旧时浪漫 2025-01-04 06:06:40

您应该查看以下示例:

http:// msdn.microsoft.com/en-us/library/aa287525%28v=vs.71%29.aspx

它应该完全符合您的要求。

编辑:

从右向左绘制可能是愚蠢的方式:

SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1, 
StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
string[] strRightToLeft = str1.Split('\n');
Array.Reverse(strRightToLeft);
e.Graphics.DrawString(String.Concat(str1), po.defaultF, Brushes.Black, rec, strF);

You should look at this example:

http://msdn.microsoft.com/en-us/library/aa287525%28v=vs.71%29.aspx

It should do exactly what you're asking.

EDIT:

Possibly stupid way of drawing from right to left:

SizeF s = e.Graphics.MeasureString(str1, po.defaultF,la1, 
StringFormat.GenericTypographic);
RectangleF rec=new RectangleF();
StringFormat strF=new StringFormat();
strF.FormatFlags=StringFormatFlags.DirectionVertical;
rec.Height=s.Width+15;
rec.Width=s.Height+5;
rec.X =0;
rec.Y=0;
string[] strRightToLeft = str1.Split('\n');
Array.Reverse(strRightToLeft);
e.Graphics.DrawString(String.Concat(str1), po.defaultF, Brushes.Black, rec, strF);
天荒地未老 2025-01-04 06:06:40

解决方案是使用 RotateTransform(90) 并且不使用 StringFormatFlags.DirectionVertical
这是代码的和平:

Rectangle rec = new Rectangle();
rec.Height = 2 * po.medF.Height;
rec.Width=100;
rec.X = 0;
rec.Y = 0;
SizeF s;
String str = "your Text";
StringFormat strf = new StringFormat();
strf.Alignment = StringAlignment.Center;    
rec.X = 0;
rec.Y = 0;
e.Graphics.TranslateTransform(X1, Y1);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(str, po.medF, Brushes.Black, rec, strf);
e.Graphics.ResetTransform();

The solution is to use RotateTransform(90) and without using the StringFormatFlags.DirectionVertical
here is the peace of code:

Rectangle rec = new Rectangle();
rec.Height = 2 * po.medF.Height;
rec.Width=100;
rec.X = 0;
rec.Y = 0;
SizeF s;
String str = "your Text";
StringFormat strf = new StringFormat();
strf.Alignment = StringAlignment.Center;    
rec.X = 0;
rec.Y = 0;
e.Graphics.TranslateTransform(X1, Y1);
e.Graphics.RotateTransform(90);
e.Graphics.DrawString(str, po.medF, Brushes.Black, rec, strf);
e.Graphics.ResetTransform();
似狗非友 2025-01-04 06:06:40

我改编了 @LarsTech 答案中的代码片段,它对我有用,如下所示:

strF = new StringFormat();
strF.Alignment = StringAlignment.Near;
strF.FormatFlags = StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

文本将被绘制,就像从下到上书写一样,左对齐。

I adapted the code snippet from @LarsTech's answer and it worked for me as below:

strF = new StringFormat();
strF.Alignment = StringAlignment.Near;
strF.FormatFlags = StringFormatFlags.DirectionVertical | StringFormatFlags.DirectionRightToLeft;
e.Graphics.TranslateTransform(rec.Right, rec.Bottom);
e.Graphics.RotateTransform(180);
e.Graphics.DrawString(str1, po.defaultF, Brushes.Black, rec, strF);

The text will be drawn as if it is written from bottom to top, left-aligned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文