C# Graphics.DrawString RectangleF 自动高度:如何找到该高度?
我正在使用 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,但文本只是溢出到外面,但宽度仍然限制文本环绕。我只是做了一些数学计算来找到高度,但我希望有更好的方法。
I am writing text over an image using Graphics DrawString method, binding my text with a RectangleF. Here is my code:
//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);
This works for some cases (ie. when header2
is only 1 line long), but my measurement
variable only measures the height of the Font, not the whole DrawString
rectangle. I do not want to set a static header2Rect
height because the heights change depending on that text.
yindex does not work because header2Rect.Height = 0
. Is there a way to see how many lines my header2
is?
Do I just need to do MeasureString
width and divide that by my boundary rectangle width, then multiply by the MeasureString
height? I'm assuming there is a better way.
Thanks
[EDIT] It looks like the height is actually 0 but the text just overflows outside, but the width still constricts the text-wrapping. I just did some math calculations to find the height, but I wish there was a better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会设置矩形高度:
You are never setting your rectangle heights: