Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. Create pixel dimensions of a text string

Create pixel dimensions of a text string

Scheduled Pinned Locked Moved WPF
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    califax2k
    wrote on last edited by
    #1

    Hi, I have the following problem: I want to know the width and height in pixels of a text string of a certain length. I tried using TextBox.GetRectFromCharacterIndex() but this didn't work. I suspect the reason is that the textBox has not been drawn before I invoked this method. I just assigned the Text property a value. The resulting rectangle is {empty}. Is there a way to obtain the pixel dimensions of a string without haven drawn or shown it. I certainly know that the result also depends on the text formating. Nevertheless, is there a way to obtain this information? thanks in advance Lars

    #pragma error( disable : * )

    realJSOPR 1 Reply Last reply
    0
    • C califax2k

      Hi, I have the following problem: I want to know the width and height in pixels of a text string of a certain length. I tried using TextBox.GetRectFromCharacterIndex() but this didn't work. I suspect the reason is that the textBox has not been drawn before I invoked this method. I just assigned the Text property a value. The resulting rectangle is {empty}. Is there a way to obtain the pixel dimensions of a string without haven drawn or shown it. I certainly know that the result also depends on the text formating. Nevertheless, is there a way to obtain this information? thanks in advance Lars

      #pragma error( disable : * )

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      You need to get the current display context, and then call the Graphics.MeasureString() method.

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      C 1 Reply Last reply
      0
      • realJSOPR realJSOP

        You need to get the current display context, and then call the Graphics.MeasureString() method.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        C Offline
        C Offline
        califax2k
        wrote on last edited by
        #3

        thanks for your reply. I forgot to mention that I want to limit myself to the WPF API. Nevermind, your answer helped me figure out the WPF-way of doing it. I am using now an instance of FormattedText und ask its Width and Height properties. Interestingly there exists a method GetMaxLineWidths() in this class but it doesn't work as I expected. It always returns {null}. Regards, Lars

        #pragma error( disable : * )

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups