C# - UWP: Win2D Measure string size

By , 2/26/2016
(0 ratings)
Win2D is Microsofts replacement for WinForms "Paint"-Event in UWP apps. This snippet shows how to measure the size of a string with the given font.
The font can be provided with the CanvasTextFormat type.

Required Namespaces

Required NuGet Pakage
Win2D.uwp http://bit.ly/1TDsCeB
private void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
    var cds = args.DrawingSession;

    var text = "Hello World!";

    var ctFormat = new CanvasTextFormat { FontSize = 30.0f, WordWrapping = CanvasWordWrapping.NoWrap };
    var ctLayout = new CanvasTextLayout(cds, text, ctFormat, 0.0f, 0.0f);

    var width = ctLayout.DrawBounds.Width;
    var height = ctLayout.DrawBounds.Height;

    //do something with width and height
Tagged with UWP, Win2D, String, Size, Measure.



Log in, to comment!