Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a string without padding

I am using the GraphicsPath.AddString() function, but it draws the text with a little space around the text. Any idea how to draw the string without that padding, only the paths of the text?

My code is like this:

GraphicsPath gp = new GraphicsPath();

gp.AddString(text, font.FontFamily, (int)font.Style, font.Size,
    boundsRectangle, format);

g.DrawPath(pen, gp);
like image 529
MHMr Avatar asked Apr 04 '13 13:04

MHMr


1 Answers

What is happening is the under the hood it is probably using Graphics.MeasureString(), from the documentation :

GDI+ adds a small amount (1/6 em) to each end of every string displayed. This 1/6 em allows >for glyphs with overhanging ends (such as italic 'f'), and also gives GDI+ a small amount >of leeway to help with grid fitting expansion.

The default action of DrawString will work against you in displaying adjacent runs:

Firstly the default StringFormat adds an extra 1/6 em at each end of each output; Secondly, when grid fitted widths are less than designed, the string is allowed to contract by up to an em. To avoid these problems:

Always pass MeasureString and DrawString a StringFormat based on the typographic >StringFormat (GenericTypographic). Set the Graphics TextRenderingHint to TextRenderingHintAntiAlias. This rendering method >uses anti-aliasing and sub-pixel glyph positioning to avoid the need for grid-fitting, and >is thus inherently resolution independent.

So it looks like you should be able to fix this using the correct StringFormat.

like image 188
Marissa Avatar answered Oct 31 '22 20:10

Marissa