snipt

Ctrl+h for KB shortcuts

C#

Get Text Width or Height

var formattedText = new FormattedText(
                    text,
                    CultureInfo.CurrentUICulture,
                    FlowDirection.LeftToRight,
                    typeFace,
                    fontSize,
                    Brushes.Black);

double width = formattedText.WidthIncludingTrailingWhitespace;

double height = formattedText.Height;
https://snippets.siftie.com/embed/ddefd31689aabaa447293f6a9d9b4981/
/raw/ddefd31689aabaa447293f6a9d9b4981/
ddefd31689aabaa447293f6a9d9b4981
csharp
C#
11
2019-03-24T04:39:25
True
False
False
Dec 04, 2013 at 04:51 AM
/api/public/snipt/98432/
get-text-width-or-height
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kt">var</span> <span class="n">formattedText</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FormattedText</span><span class="p">(</span> </span><span id="L-2"><a name="L-2"></a> <span class="n">text</span><span class="p">,</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">CultureInfo</span><span class="p">.</span><span class="n">CurrentUICulture</span><span class="p">,</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">FlowDirection</span><span class="p">.</span><span class="n">LeftToRight</span><span class="p">,</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">typeFace</span><span class="p">,</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">fontSize</span><span class="p">,</span> </span><span id="L-7"><a name="L-7"></a> <span class="n">Brushes</span><span class="p">.</span><span class="n">Black</span><span class="p">);</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a><span class="kt">double</span> <span class="n">width</span> <span class="p">=</span> <span class="n">formattedText</span><span class="p">.</span><span class="n">WidthIncludingTrailingWhitespace</span><span class="p">;</span> </span><span id="L-10"><a name="L-10"></a> </span><span id="L-11"><a name="L-11"></a><span class="kt">double</span> <span class="n">height</span> <span class="p">=</span> <span class="n">formattedText</span><span class="p">.</span><span class="n">Height</span><span class="p">;</span> </span></pre></div> </td></tr></table>