I have code, similar to the following:
string myText = "This is a test";
Font myFont = new Font("Arial", 10);
Rectangle rect = new Rectangle(10,10,100,100);
Graphics g = e.Graphics;
g.DrawString(myText, myFont, rect.X, rect.Y);
Although this works, what I would like to do is have the text vertically and horizontally centered within the dimensions of the rectangle. In addition, if its possible, I’d like to wrap the text if its too big to fit on one line. How do I do this in C#/GDI+?
I would use one of the
DrawStringOverloads that takes aStringFormatYou would have to check the Length of your Text usingIn looking further at theMeasureStringto make sure it would fit and wrap it yourself.StringFormatFlagsit has a NoWrap Flag implying that the default is to wrap:from last link: