Evgeny Pokhilko's Weblog

Programmer's den

Measure Size Of Rendered Text In WPF

Problem: Get the size that arbitrary text will take to adjust width, height of a control or control wrapping.

Solution:

FormattedText formattedText = new FormattedText(text, 
  Thread.CurrentThread.CurrentCulture, 
  FlowDirection.LeftToRight, 
  new Typeface(fontFamilyName), 
  fontSize, 
  Brushes.Black); 
return formattedText.Width;

October 20, 2010 Posted by | .NET, WPF | , | Leave a comment