[C#]計算字串的寬度

  • 3254
  • 0

摘要:[C#]計算字串的寬度

這是以前的片段程式碼囉..給大家參考參考~(恩...我以前寫C#的= =)


public static int CharWidth(string str, System.Drawing.Font font)
{
	string target = str;
	System.Windows.Forms.Control control = new System.Windows.Forms.Control();
	System.Drawing.Graphics g = control.CreateGraphics();
	System.Drawing.SizeF sizef = g.MeasureString(target, font);
	g.Dispose(); // Necesary to destroy the graphics object
	int charWidth = (int)sizef.Width; // gets the width of the size object.
	return charWidth;
}