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
  1. Home
  2. General Programming
  3. C#
  4. measure string

measure string

Scheduled Pinned Locked Moved C#
questiongraphics
5 Posts 4 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.
  • N Offline
    N Offline
    nryk
    wrote on last edited by
    #1

    hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).

    A P E 3 Replies Last reply
    0
    • N nryk

      hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).

      A Offline
      A Offline
      AndrewVos
      wrote on last edited by
      #2

      TextRenderer should work for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx[^]

      www.andrewvos.com

      N 1 Reply Last reply
      0
      • A AndrewVos

        TextRenderer should work for this. http://msdn.microsoft.com/en-us/library/system.windows.forms.textrenderer.measuretext.aspx[^]

        www.andrewvos.com

        N Offline
        N Offline
        nryk
        wrote on last edited by
        #3

        i'm using 1.1 Framework, so i think i can't use it :-(

        1 Reply Last reply
        0
        • N nryk

          hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You don't need to receive a Graphics object to use one in your method. What you could do is create a 1 by 1 bitmap, and then retrieve the Graphics object from that.

          using (Bitmap img = new Bitmap(1,1))
          {
          float resolution = 300;
          img.SetResolution(resolution, resolution);
          using (Graphics g = Graphics.FromImage(img))
          {
          // We have a graphics object that we can use now.
          }
          }

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • N nryk

            hi how can i measure string height without using Graphics.MeasureString()? i've tried to use GraphicsPath and AddString() then to GetBounds() but that gives me the exact height, but i need the height like MeasureString() function. the reason i can't use Graphics is that i need to calculate something about the string height in a function that doesn't get Graphics variable. (in the function that does have the Graphics variable i'm using 300 dpi).

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            The Graphics context dictates the way the Fonts are rendered. It is not possible to accurately measure a string without knowing where it is going. All other option require guessing in the method. If you want to guess you can look-up the font characteristics and roll your own, or as suggested earlier create a fake graphics context that does not match the device. Unfortunately, it will be buggy in some edge cases that may be hard to detect in development but the client will notice immediately. The best thing to do is to pass a reference to the Graphics context everywhere you want to draw to a device.

            Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

            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