Sử dụng Graphics.MeasureString()
Từ MSDN: http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx
private void MeasureStringMin(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Nguồn
2012-12-17 12:41:14
duplipcate có thể có: http://stackoverflow.com/questions/7714022/how-to-get-a-string-width – Default
Có thể trùng lặp [Làm cách nào để chuyển đổi độ dài chuỗi thành đơn vị pixel?] (Http: //stackoverflow.com/questions/451903/how-can-i-convert-a-string-length-to-a-pixel-unit) –