简体   繁体   English

使用graphics.DrawString方法进行绘制时文本被剪切

[英]Text getting clipped when drawing using graphics.DrawString method

I am using the graphics.MeasureString method to measure the text size. 我正在使用graphics.MeasureString方法来测量文本大小。 And based on the measured text size, I was the draw the string using the graphics.MeasureString. 然后根据测得的文本大小,使用graphics.MeasureString绘制字符串。 But in that I was using the StringFormat to measure and draw the string. 但是我当时使用StringFormat来测量和绘制字符串。 But I found the text clipping problem in some text like "left". 但是我在诸如“ left”之类的文本中发现了文本剪切问题。

Please find the code snippet below, 请在下面找到代码片段,

string text = "Left";
Font font = new System.Drawing.Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height), format);

Please find the text clipping while drawing in below screen shot, 请在下面的屏幕截图中进行绘制时找到文本剪辑,

在此处输入图片说明

Can you please suggest how to solve this issue? 您能否提出解决此问题的建议?

Try the following. 尝试以下方法。 I have removed the StringFormat from the code. 我已经从代码中删除了StringFormat It works. 有用。

string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
//StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width, size.Height));

Edit1 编辑1

I have modified the answer as per OP's request to use StringFormat enum. 我已经根据OP的使用StringFormat枚举的请求修改了答案。

string text = "Left";
Font font = new Font("Segoe UI Semibold", 9F);
StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
SizeF size = e.Graphics.MeasureString(text, font, 100, format);
e.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new RectangleF(10, 10, size.Width + 5, size.Height), format);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM