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 のように見えますが、テキストは外側にはみ出していますが、幅は依然としてテキストの折り返しを制限しています。高さを見つけるためにいくつかの数学計算を行っただけですが、もっと良い方法があればいいのにと思います。