2

I want to measure the length of a string into pixel units. I searched the web for 2 days but got no luck. Finally, I found some code snippet a few minutes ago from a blog and modified it a little bit. Here's my function:

private void cmdMeasure_Click(object sender, EventArgs e)
{
    Font fntStyle = new Font("Arial", 16, FontStyle.Regular, GraphicsUnit.Pixel);
    Size textSize = TextRenderer.MeasureText(str2measure.Text, fntStyle);
    MessageBox.Show(textSize.ToString());
}

Question:

What is the unit of 16? Is it em, pt, or the unit of GraphicsUnit enum? I don't really get the description defined by c# "the em-size of the new font in the units specified by the unit parameter."

Does the TextRenderer.MeasureText method include the spaces between the characters in its measurement?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ELM
  • 529
  • 2
  • 7
  • 19
  • 3
    Following the docs [here](http://msdn.microsoft.com/en-us/library/ms141987.aspx) and [here](http://msdn.microsoft.com/en-us/library/system.drawing.graphicsunit.aspx) shows that the 16 is in units of pixels, since the parameter `GraphicsUnit.Pixel` is supplied to the Font constructor. – Adam Mihalcin Apr 18 '12 at 03:47
  • It would be in Pixels thanks to the GraphicsUnit.Pixel, and would mean for most fonts - the height of the capital letter W. Characters can naturally overlap and the measurement will return the final size from left-side to right-side plus edge-padding. You can also use Graphics.MeasureCharacterRanges. – SimpleVar Apr 18 '12 at 03:52
  • http://stackoverflow.com/questions/451903/how-can-i-convert-a-string-length-to-a-pixel-unit – Govind Malviya Apr 18 '12 at 03:56

3 Answers3

3

What is the unit of 16? Is it em, pt, or the unit of GraphicsUnit enum? I don't really get the description defined by c# "the em-size of the new font in the units specified by the unit parameter."

As others have said, it's pixels since you pass GraphicsUnit.Pixel.

Does the TextRenderer.MeasureText method include the spaces between the characters in its measurement?

Yes. It gives you the dimensions of the bounding box around the whole block of text.

Larsenal
  • 49,878
  • 43
  • 152
  • 220
  • I tried to measure the string "a" and it returned a width of 17 and height of 18. So are you saying that the width or height minus 16 equals to the thickness of the bounding box? – ELM Apr 18 '12 at 04:11
  • The 17x18 dimensions simply mean that an "a" drawn on screen in Arial with a size 16 font would fit nicely inside a 17x18 rectangle. Think of it as the area "used" to display the text. – Larsenal Apr 18 '12 at 04:23
  • Yes. Check the docs here: http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx – Larsenal Apr 18 '12 at 06:32
1

TextRenderer.MeasureString does take in account the spaces. In your example, the number 16 is in pixels.

Although, I'm not sure you are measuring your string correctly. I think you need to get your Font from your str2measure textbox:

Size textSize = TextRenderer.MeasureText(str2measure.Text, str2measure.Font);
Brad Rem
  • 6,036
  • 2
  • 25
  • 50
  • I tried to change the font property of str2measure.Text but the function returned the same results. – ELM Apr 18 '12 at 04:13
0

Ans Q1. It is pixels only as you are passing GraphicsUnit.Pixel

Ans Q2. It is considering the entire text block no matter what is the content even white spaces.

Shaikh Farooque
  • 2,620
  • 1
  • 19
  • 33