Ohad's Blog

Lets talk about .net !

Mirror at:
blogs.microsoft.co.il

News

         Ohad Israeli's Facebook profile

Grab this badge here!

C# Code Snippts

Favorite Blogs

Israeli .Net Bloggers

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);
ppd.ShowDialog();

Comments

Jason Haley said:

# July 30, 2006 11:39 AM

Mark said:

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

# January 2, 2007 9:47 AM

Peter Alber said:

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

# January 9, 2008 5:58 PM

Saragani said:

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?

# May 4, 2008 3:43 AM

Dirk said:

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.

# June 4, 2008 6:49 PM

jlevin said:

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.

# January 19, 2009 7:40 AM

Chul said:

forget GDI+ about this.

Use GDI for measuring strings.

it's the only way of avoiding the aging process.

# September 3, 2009 6:40 AM

Eric Tsai said:

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

# September 19, 2009 5:56 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)