Tigraine
Daniel Hoelbling talks about .NET

GDI Drawing: String with word-wrap

January 3rd, 2009 . by Daniel Hölbling

GDI drawing is magic and not very well documented. Finding out how to make GDI draw a string inside a fixed width without constraining the height took me about 20 minutes, hopefully this post saves you 20 minutes of your life nobody will give you back :) .

The problem with Graphics.DrawString() is that you can either supply a PointF that will be used as the origin, or you supply a RectangleF as layout rectangle. The rectangle’s sizes and position will be forced upon the text and word-wrap will happen to fit the text inside the rectangle, anything not fitting in the rectangle (given the font-size) will be cut off.
So if you don’t want to confine width or the height, setting that property on the RectangleF to 0 will make GDI not "cut off” but expand the rectangle as needed.

Needless to say that this behavior isn’t mentioned on the MSDN page for DrawString, that may have saved me time.


View Comments to “GDI Drawing: String with word-wrap”

  1. comment number 1 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  2. comment number 2 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  3. comment number 3 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  4. comment number 4 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  5. comment number 5 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  6. comment number 6 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  7. comment number 7 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  8. comment number 8 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  9. comment number 9 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  10. comment number 10 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  11. comment number 11 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  12. comment number 12 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it’s rendered?

  13. comment number 13 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  14. comment number 14 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  15. comment number 15 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  16. comment number 16 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  17. comment number 17 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  18. comment number 18 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  19. comment number 19 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  20. comment number 20 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  21. comment number 21 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  22. comment number 22 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  23. comment number 23 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  24. comment number 24 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object. rnrnSizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  25. comment number 25 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  26. comment number 26 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  27. comment number 27 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  28. comment number 28 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  29. comment number 29 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  30. comment number 30 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  31. comment number 31 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  32. comment number 32 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  33. comment number 33 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  34. comment number 34 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  35. comment number 35 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  36. comment number 36 by: Daniel Hu00f6lbling

    Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.

  37. comment number 37 by: Stater136

    Have you found a way to determine what the height of the resulting box is once it's rendered?

  38. comment number 38 by: Stater136

    Never mind. I figured out how to do this with measure string and using a StringFormat object.

    SizeF textSize = gfx.MeasureString(text, font, rectangle.Size, stringFormat);

  39. comment number 39 by: Daniel Hölbling

    Thanks for sharing.
    I would also have suggested feeding the same parameters you give to DrawString to MeasureString.

Leave a Reply

Name

Mail (never published)

Website

blog comments powered by Disqus