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.