Determining the TextWidth of a String

Is there a way to determine the TextWidth (NOT Length) of a string that is not currently in a Graphics object?

I have a string array and want to determine which element will require the widest display area. Using String.Length won’t work since it returns the number of characters and not all characters are the same width (in most fonts.)

My initial thought was to create a local graphics object and somehow put the strings, one by one, into it and get the TextWidth of each but I can’t seem to figure out how.

Is this the right path or is there another way to approach the question?

Any help would be appreciated.

Yes there is a right path. You will need the correct graphics context for where you’re going to display the text. This is what Window.BitmapForCaching can provide.

Create a new Picture (1x1 will do it), and use its Graphics to get the width of each element.

This, per the Window.BitmapForCaching documentation, is the wrong path.

Use this method instead of “New Picture” in order to get a Picture image that is suitable for HiDPI displays.

Edit: Had to put the quote back in because Discourse :roll_eyes:

I defer to your judgement.

Thanks, I knew there had to be a correct way to do it but I just didn’t now what it was.