【发布时间】:2017-03-22 04:13:16
【问题描述】:
我有一个示例字符串,我计算它的大小并通过下面的代码在表单中绘制字符串,
public partial class Form1 : Form
{
Rectangle textRectangle;
string text = "testing the size 1 testing the size 1 testing the size 1 testing the size 1";
public Form1()
{
InitializeComponent();
Size textSize = this.CreateGraphics().MeasureString(text, this.Font, 100).ToSize();
textRectangle = new Rectangle(0, 0, textSize.Width, textSize.Height);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (SolidBrush brush = new SolidBrush(Color.Blue))
{
e.Graphics.DrawString(text, this.Font, brush, textRectangle);
}
}
}
我的问题是从简单字符串计算的 textRectangle 不足以以形式绘制。请参考附图,某些给定的字符串未绘制。
谁能更新我,为什么从 MeasureString() 计算的大小不足以使用 DrawString() 方法绘制?
【问题讨论】:
-
对我来说很好。并不是说您应该使用
CreateGraphics(),如果您决定使用,您应该处置您创建的Graphics实例。但我无法重现您的投诉。Font设置为什么?您使用的是什么操作系统版本?屏幕显示分辨率和字体缩放比例是多少? -
@PeterDuniho - 查看已解决的答案。