Inconsistencies in measuring strings and character ranges RRS feed

  • Question


    I'm having trouble measuring character ranges in a WinForms application.

    I would have thought that if I measured a complete string using Graphics.MeasureCharacterRanges(), and then measured it letter by letter using the same method and adding up the widths of the individual characters, the results would be the same. They are not however.


    I tried to do the same using TextRenderer.MeasureString() and Graphics.MeasureString() and they both return a different result for the whole string  vs. the seperate character lengths added up.


    I tested it simply using the word "TEST". Can anybody explain what I am doing wrong and what function I can use to get the true length of a character so that the length of "T" + "E" + "S" + "T" = the length of "TEST"?




    Friday, November 14, 2008 10:33 PM


All replies

  • Ok, I've managed to work around this problem but I don't understand why it is the case so if someone more knowledgeable can explain it that would be great.

    I've discovered that if I set the Graphics.PageUnit property to INCH before performing the measuring, Graphics.MeasureCharacterRanges() returns the same value for the whole word "TEST" and the individual characters widths added up. I can then set the Graphics.PageUnit property back to DISPLAY to do the drawing.

    Does anyone know why it doesn't measure consistently using PageUnit.PIXEL?

    I don't want this to break my app in the future.....


    Friday, November 14, 2008 11:03 PM
  • Saturday, November 15, 2008 1:53 PM