我正在使用 Graphics DrawString 方法在图像上写入文本,将我的文本与 RectangleF 绑定。这是我的代码:
//Write header2
RectangleF header2Rect = new RectangleF();
header2Rect.Width = 600;
header2Rect.Location = new Point(30, 105);
graphicImage.DrawString(header2, new Font("Gotham Medium", 28, FontStyle.Bold),brush, header2Rect);
//Write Description
RectangleF descrRect = new RectangleF();
descrRect.Width = 600;
int measurement = ((int)graphicImage.MeasureString(header2, new Font("Gotham Medium", 28, FontStyle.Bold)).Height);
var yindex = int.Parse(105 + header2Rect.Height.ToString());
descrRect.Location = new Point(30, 105+measurement);
graphicImage.DrawString(description.ToLower(), new Font("Gotham", 24, FontStyle.Italic), SystemBrushes.WindowText, descrRect);
这适用于某些情况(即header2
只有 1 行长),但我的measurement
变量只测量字体的高度,而不是整个DrawString
矩形。我不想设置静态header2Rect
高度,因为高度会根据该文本而变化。
yindex 不起作用,因为header2Rect.Height = 0
. 有没有办法查看我的行header2
数?
我只需要做MeasureString
宽度并将其除以我的边界矩形宽度,然后乘以MeasureString
高度吗?我假设有更好的方法。
谢谢
[编辑] 看起来高度实际上是 0,但文本只是溢出到外面,但宽度仍然限制了文本换行。我只是做了一些数学计算来找到高度,但我希望有更好的方法。