2

I need to change distance between characters when adding string in GraphicsPath. What is the best way to do this?

Viktor
  • 164
  • 1
  • 14

1 Answers1

0

You could draw each character individually and adjust the distance between characters, something like.

public void DrawText(string text, Point at, float distanceBetweenChars, FontFamily fontFamily, float fontSize, Graphics graphics)
{
    float currentX = at.X;
    for (int i = 0; i < text.Length; i++)
    {
        using (var path = new GraphicsPath())
        {
            path.AddString(text.Substring(i, 1), fontFamily, (int)FontStyle.Regular, fontSize,
                               new Point((int)currentX, at.Y),
                               StringFormat.GenericDefault);
                RectangleF bounds = path.GetBounds();
                currentX += bounds.Width + distanceBetweenChars;
                graphics.FillPath(new SolidBrush(Color.Black), path);
            }
        }
    }
}
chrisb
  • 937
  • 1
  • 10
  • 22
  • This is not a general solution, it could cause problems of alignment with some languages, arabic, for instance. – IssamTP Jul 14 '20 at 12:38