Search code examples
wpftextblock

How convert string to pixels


I have TextBlock with some Text="AAAAAAAA", FontFamily="Arial", FontSize="16". How I can convert Text to WPF pixels?


Solution

  • You could use FormattedText for measuring:

    FormattedText formattedText = new FormattedText(
        "AAAAAAAA",
        CultureInfo.CurrentCulture,
        FlowDirection.LeftToRight,
        new Typeface("Arial"), 16, Brushes.Black
    );
    MessageBox.Show(formattedText.Width.ToString());