A
A
Alexander Ter2018-03-12 09:36:34
C++ / C#
Alexander Ter, 2018-03-12 09:36:34

How to calculate line breaks in C# Graphics?

How to determine how lines wrap when rendering multi-line text?
The text is rendered:

var _numberLines = GetNumOfLines($"{_property.Текст}", (int)(PicBoxWidth - 6), _font);
var _offcet = _font.Height * _numberLines;
g.DrawString($"{_property.Текст}", _font, Brushes.Black, new RectangleF(5, 0, PicBoxWidth - 6, _offcet), new StringFormat(StringFormatFlags.LineLimit));

where the number of rows is determined by:
private int GetNumOfLines(string multiPageString, int wrapWidth, Font fnt)
{
        var sfFmt = new StringFormat(StringFormatFlags.LineLimit);
        using(var g = Graphics.FromImage(New Bitmap(1, 1)))
       {
            var iHeight = g.MeasureString(multiPageString, fnt, wrapWidth, sfFmt).Height;
            var iOneLineHeight = g.MeasureString("Z", fnt, wrapWidth, sfFmt).Height;
            return (int)(iHeight / iOneLineHeight)
        }
}

Does anyone have any thoughts?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question