I have a string like this:
string s = \"This is my string\";
I am creating a Telerik report and I need to define a textbox
tha
Without using of a control or form:
using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(new Bitmap(1, 1)))
{
SizeF size = graphics.MeasureString("Hello there", new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point));
}
Or in VB.Net:
Using graphics As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(New Bitmap(1, 1))
Dim size As SizeF = graphics.MeasureString("Hello there", New Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Point))
End Using
You can create an instance of a graphics object an use the MeasureString()
method. But you will need to pass it the font name, font size, and other info.
Depends on the font, too. String length isn't sufficient.
In this case, I usually use a dirty, but simple way:
Label
that its AutoSize
property is true
-dirty work-.Width
for a specific string, I set it to the Label.Text
.Width
of the Label
will give me the correct value.Size textSize = TextRenderer.MeasureText("How long am I?", font);