Easy way to center multi-line text in Graphics.DrawString ??

Centering text is easy with a single line of text:
textLeft = (Graphics.Width - Graphics.StringWidth(caption)) / 2

Then draw the string using textLeft as the x coordinate for the string in Graphics.DrawString.

However, there is no way (that I can see) to calculate the proper width of the string in Graphics.StringWidth(caption), if the text represented by caption will end up being wrapped in the Graphics Object.

Is there a way to calculate the effective StringWidth for a string that will be wrapped?

Another way to center text is to use a multiline label and DrawInto it in the graphics object.

I use my own routine to wrap the text and then draw each line individually. Not only does that allow me to center the text, it allows me to control line spacing.

  • Karen

Both are great suggestions. Thanks! It sounds like at least I was correct in understanding there aren’t any specific Graphics calls to accomplish that.