■ 문자열 크기를 측정하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System.Drawing; #region 문자열 크기 측정하기 - MeasureStringSize(graphics, font, source) /// <summary> /// 문자열 크기 측정하기 /// </summary> /// <param name="graphics">그래픽스</param> /// <param name="font">폰트</param> /// <param name="source">소스 문자열</param> /// <returns>문자열 크기</returns> public Rectangle MeasureStringSize(Graphics graphics, Font font, string source) { StringFormat stringFormat = new StringFormat(); RectangleF rectangleF = new RectangleF(0, 0, 1000f, 1000f); CharacterRange[] characterRangeArray = { new CharacterRange(0, source.Length) }; Region[] regionArray = new Region[1]; stringFormat.SetMeasurableCharacterRanges(characterRangeArray); regionArray = graphics.MeasureCharacterRanges(source, font, rectangleF, stringFormat); rectangleF = regionArray[0].GetBounds(graphics); return new Rectangle(0, 0, (int)(rectangleF.Right + 1f), (int)(rectangleF.Bottom + 1f)); } #endregion |