Monday, June 15, 2009

ASP.Net - Calculate label width dynamically using the text

Sometimes, we have to calculate a label's width dynamically , depending on the provided text. In order to do that, use the following,

float width = Graphics.FromImage(new Bitmap(1, 1)).MeasureString("Hello world!", new Font("Verdana", 14)).Width

You will have to mention the font family and the desired size.

1 comment: