DrawString / MeasureString Offset Problem Solved !

When you are trying to use DrawString and MeasureString for example to print something it seems that there is a little offset to the right and the left of the text, using MeasureString doesn’t seems to do its job.

This is how it will look:

And this is how you want it to look:

In order to solve the measuring problem all you have to do is use StringFormat with GenericTypographic and send it both to the MeasureString & to the DrawString as additional parameter.

Microsoft’s KB-307208 explains in more detail what is going on behind the gdi plus DrawString & MeasureString methods.

— Using the above method in a print preview example —

PrintPreviewDialog ppd = new PrintPreviewDialog();
System.Drawing.Printing.PrintDocument pd = new PrintDocument();
pd.PrintPage +=delegate(object printSender, PrintPageEventArgs printEventArg)
    string str = "Some String";
    Font font = new Font("Arial", 12);
    PointF location = new PointF(10, 10);
    PointF origin = new PointF(0, 0);

    StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic);

    SizeF textArea = printEventArg.Graphics.MeasureString(str, font, origin, sFormat);
    printEventArg.Graphics.FillRectangle(System.Drawing.Brushes.Red, new RectangleF(location, textArea));
    printEventArg.Graphics.DrawString(str, font, System.Drawing.Brushes.Black, location,sFormat);

ppd.Document = pd;
ppd.PrintPreviewControl.Zoom = 4.0;
ppd.ClientSize = new Size(600, 300);


  • Note that when using this method with a string containing Hebrew characters, MeasureString returns 0 for its Height and Width.

  • Also note if the string ends with a space " " MeasureString returns 0 for its Height and Width.

  • I want to calculate a character width.

    (I have a string and I ru non each char and check its width).

    by using the regular method each caracter is being something like 1.5 of its size (because of the padding).

    When using StringFormat I can't get the size of the " " char and hebrew chars (like already mentioned).

    Any other solution?

  • Even when using the above solution I still got issues with spacing/offset. So I was a bit disappointed with such a promising title.
    Anyway, i found out that using the genericmonospace font (default= "Courier New") solved it for me as workaround for my situation.

  • I still get a small offset even with GenericTypographic and a monospace font. It is nowhere near as noticeable as before but if you e.g. print a sentence once letter by letter and once as a full string then the sentence printed letter by letter will be slight shorter.

  • forget GDI+ about this.
    Use GDI for measuring strings.
    it's the only way of avoiding the aging process.

  • For " ", you must do stringFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;

  • Thanks for this post.
    It works by using
    sf = StringFormat.GenericTypographic
    sf.Trimming = StringTrimming.None
    sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces
    gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
    But spaces with underline style are not painted with underline. I will search for any solution again...

Comments have been disabled for this content.