Unexpected behavior of MeasureString

M

matko

When the height of some text is to be calculated, I get different
height values depending on where transformation is done. This only
occurs for some fonts. F.ex., when height is calculated for Verdana,
the size is the same no matter where transformation is done. With
Times New Roman, size differs. Can someone explain this behavior? How
can rotation alter the height of text only rendered with certain
fonts?

The following is the code that is (without modification) ready to be
pasted in the Paint event handler of the main form. It will display
the height of the text, whose size is measured, on the surface of the
form. Run it once and note the displayed value on the form.
Afterwards, change the location of the code within "<Area One>" to
"<Area Two>" and run the code again. The size of the measured text is
now different! If you change the font to Verdana, the size will remain
the same...... Fishy.

Code:

string measuredText = "99";
this.Font = new System.Drawing.Font("Times New Roman", 22.0f);
e.Graphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;

System.Drawing.Size textSize = e.Graphics.MeasureString(measuredText,
Font, int.MaxValue,
System.Drawing.StringFormat.GenericTypographic).ToSize();

using (System.Drawing.Drawing2D.Matrix matrix = new
System.Drawing.Drawing2D.Matrix())
{
matrix.Rotate(180.0f, System.Drawing.Drawing2D.MatrixOrder.Append);
matrix.Translate(textSize.Width, textSize.Height,
System.Drawing.Drawing2D.MatrixOrder.Append);

// <Area One>
e.Graphics.Transform = matrix;
// </Area One>

int measuredHeight = e.Graphics.MeasureString(measuredText, Font,
int.MaxValue,
System.Drawing.StringFormat.GenericTypographic).ToSize().Height;
e.Graphics.DrawString(measuredHeight.ToString(), this.Font, new
System.Drawing.SolidBrush(System.Drawing.Color.Green), new
System.Drawing.PointF(0.0f, 0.0f),
System.Drawing.StringFormat.GenericTypographic);

// <Area Two>

// </Area Two>
}
 
M

matko

Sorry for the weird formatting + the "Show quoted text"-thing when
viewing this via Google Groups.
D'oh!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top