GDI Drawing: String with word-wrap
January 3rd, 2009 . by Daniel HölblingGDI 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.



Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
Have you found a way to determine what the height of the resulting box is once it’s rendered?
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Thanks for sharing.nI would also have suggested feeding the same parameters you give to DrawString to MeasureString.
Have you found a way to determine what the height of the resulting box is once it's rendered?
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);
Thanks for sharing.
I would also have suggested feeding the same parameters you give to DrawString to MeasureString.