Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center text output from Graphics.DrawString()

I'm using the .NETCF (Windows Mobile) Graphics class and the DrawString() method to render a single character to the screen.

The problem is that I can't seem to get it centred properly. No matter what I set for the Y coordinate of the location of the string render, it always comes out lower than that and the larger the text size the greater the Y offset.

For example, at text size 12, the offset is about 4, but at 32 the offset is about 10.

I want the character to vertically take up most of the rectangle it's being drawn in and be centred horizontally. Here's my basic code. this is referencing the user control it's being drawn in.

Graphics g = this.CreateGraphics();  float padx = ((float)this.Size.Width) * (0.05F); float pady = ((float)this.Size.Height) * (0.05F);  float width = ((float)this.Size.Width) - 2 * padx; float height = ((float)this.Size.Height) - 2 * pady;  float emSize = height;  g.DrawString(letter, new Font(FontFamily.GenericSansSerif, emSize, FontStyle.Regular),             new SolidBrush(Color.Black), padx, pady); 

Yes, I know there is the label control that I could use instead and set the centring with that, but I actually do need to do this manually with the Graphics class.

like image 901
Adam Haile Avatar asked Aug 11 '08 17:08

Adam Haile


People also ask

How do I center a string in C#?

double theObject = Math. PI; string test = string. Format("Now '{0:F4}' is used.", theObject. Center(10));

What is the use of DrawString in Java?

The Graphics class provides three methods that draw text on a component or an image. The drawString() method, shown below, takes as parameters an instance of the String class containing the text to be drawn, and two integer values specifying the coordinates where the text should start.

Which method draws font on the graphics window?

DrawString(String, Font, Brush, PointF) Draws the specified text string at the specified location with the specified Brush and Font objects.


2 Answers

I'd like to add another vote for the StringFormat object. You can use this simply to specify "center, center" and the text will be drawn centrally in the rectangle or points provided:

StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Center; 

However there is one issue with this in CF. If you use Center for both values then it turns TextWrapping off. No idea why this happens, it appears to be a bug with the CF.

like image 59
Quibblesome Avatar answered Nov 05 '22 13:11

Quibblesome


To align a text use the following:

StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; sf.Alignment = StringAlignment.Center; e.Graphics.DrawString("My String", this.Font, Brushes.Black, ClientRectangle, sf); 

Please note that the text here is aligned in the given bounds. In this sample this is the ClientRectangle.

like image 42
Chris Hughes Avatar answered Nov 05 '22 13:11

Chris Hughes