0

I want to achieve this in WinRT app.

I know I can use SharpDX to render the text, you can check out my CodeProject article. I want to know how to create a white background image of size with capacity to accommodate the text with font-family Segoe UI and font size of 16?

I've found how to create white image. But how can I calculate the size of the long string (like 2000+ characters), which is going to be render on white image ?

I've checked how to calculate the textbock height and width in on load if i create textblock from code?, but I am getting incorrect value. I don't know what to pass as arguments in Arrange(...) & Measure(...) methods. I am passing Window.Current.Bounds as parameter. I want something like this.

private double GetHeight(string Text, string fontFamily, double fontSize)
{
    //TODO:
}

UPDATE 1

According to Filip's suggestion I tried this, but it's also giving me incorrect value.

TextBlock textBlock = new TextBlock();
private double GetHeight(string Text)
{
    var scroller = new ScrollViewer();
    scroller.Height = Window.Current.Bounds.Height;
    scroller.Width = Window.Current.Bounds.Width;
    scroller.Content = textBlock;

    textBlock.Text = Text;
    textBlock.FontFamily = new FontFamily("Segoe UI");
    textBlock.FontSize = 40;
    textBlock.TextWrapping = TextWrapping.Wrap;
    textBlock.Arrange(new Rect(0, 0, 9000000000, 9000000000));
    textBlock.Measure(new Size(9000000000, 9000000000));

    root.Children.Add(scroller);

    return textBlock.ActualHeight; //return 478.82919311523437
}

// root is the Root grid of the app, there's nothing in XAML
private void root_Tapped_1(object sender, TappedRoutedEventArgs e)
{
    var aa = textBlock.ActualHeight; //return 2766.5732421875
}
Community
  • 1
  • 1
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
  • Have you searched for solutions that can measure text? (I think that's what you're referring to) – WiredPrairie Sep 14 '13 at 20:44
  • I tried [this](http://stackoverflow.com/questions/10556019/how-to-calculate-the-textbock-height-and-width-in-on-load-if-i-create-textblock) but it gives me incorrect value. – Farhan Ghumra Sep 15 '13 at 02:01
  • Incorrect means it is not the actual value which is supposed to. I tried by creating manually an image in paint and it had the height nearly 1000+ pixel but when I use arrange & measure it gives nearly 700+ pixels I doubt about the argument of arrange & measure. I am passing `Window.Current.Bounds` as parameter. – Farhan Ghumra Sep 15 '13 at 16:09
  • Calling `arrange` before `measure`? I don't think that's the right sequence. Not that I'm guaranteeing it will give the right results. – afaolek Jul 28 '15 at 09:33

0 Answers0