우리는 WPF에서 간단한 텍스트 측정을 수행하기 위해 다음과 같은 유틸리티 클래스를 사용하고 있습니다 :
이
public static class TextUtilities
{
public static Size MeasureText(string text,
FontFamily family, double size, FontStyle style, FontWeight weight, FontStretch stretch, TextFormattingMode formattingMode)
{
FormattedText formattedText = new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(family, style, weight, stretch),
size,
Brushes.Black,
null,
formattingMode);
return new Size(formattedText.Width, formattedText.Height);
}
public static Size MeasureText(string text, TextBlock textBlock)
{
return MeasureText(text,
textBlock.FontFamily,
textBlock.FontSize,
textBlock.FontStyle,
textBlock.FontWeight,
textBlock.FontStretch,
TextOptions.GetTextFormattingMode(textBlock));
}
public static Size MeasureText(string text, Control control)
{
return MeasureText(text, control.FontFamily, control.FontSize,
control.FontStyle, control.FontWeight, control.FontStretch,
TextOptions.GetTextFormattingMode(control));
}
}
어쩌면 모니터는 96dpi보다 높은 해상도를 가지고? WPF는 해상도에 독립적이지만 'Graphics'는 그렇지 않습니다. 차이점을 설명 할 수 있습니다. 내가 그것에 대해 확신하지 못하기 때문에, 나는 이것을 대답이 아니라 주석으로 남겨 두었습니다. –
아마도 도움이 될 것 같습니까? http://stackoverflow.com/questions/632445/measuring-text-in-wpf – series0ne