Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DrawString Centered Text in C#

Tags:

c#

Today I am trying to draw a string on a form, but need to be drawn exactly in the middle, I have sought functions but I do not see the parameters of the rectangle centered or obtained automatically.

If someone would kindly give me some function in which to center the text automatically. The commands I use are:

 gfx.DrawString(line, printFont, myBrush, leftMargin, YPosition(), new StringFormat());

Thanks for your Help

like image 713
Jonathan Edgardo Avatar asked Jun 11 '26 15:06

Jonathan Edgardo


1 Answers

You can set parameters using the StringFormat object you're passing in.

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;      // Horizontal Alignment
stringFormat.LineAlignment = StringAlignment.Center;  // Vertical Alignment

How to: Align Drawn Text

like image 181
Grant Winney Avatar answered Jun 14 '26 05:06

Grant Winney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!