Using Win2D is it possible to get text size without a drawingSession?

120 views Asked by At

This question is different from this question - How to calculate the size of a piece of text in Win2D - because that method has the result attached to the draw event of a Win2D CanvasControl. Specifically, it requires the DrawingSession of the arguments.

I want to know if it is possible to know the size of the text without having the draw function called. This is desired because I need the various CanvasControls I am using widths or heights to be able to be set by the size of the text.

1

There are 1 answers

0
Michael S. Scherotter On

Create an instance of the CanvasTextLayout class with the text and format and get the DrawBounds property of it.

using (var layout = new CanvasTextLayout(creator, "Hello world", format, 200, 100))
{
   var bounds = layout.DrawBounds;
}