473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Unexpected behavior of MeasureString

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).To Size();

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).To Size().Height;
e.Graphics.DrawString(measuredHeight.ToString(), this.Font, new
System.Drawing.SolidBrush(System.Drawing.Color.Gre en), new
System.Drawing.PointF(0.0f, 0.0f),
System.Drawing.StringFormat.GenericTypographic);

// <Area Two>

// </Area Two>
}

May 9 '07 #1
1 1510
Sorry for the weird formatting + the "Show quoted text"-thing when
viewing this via Google Groups.
D'oh!

May 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
1
by: Yaron | last post by:
Hi, I made a procedure which receives a Graphics parameter, and with that Graphics parameter, performs various things. The area of confusion here is that when I pass the procedure a Graphics...
2
by: Stuart Norris | last post by:
Dear Group, I have a question about the result from ‘graphics.MeasureString' when using a mono-spaced font. Firstly I assume that Courier New font is mono-spaced, ie each character takes up the...
5
by: Marc | last post by:
I want to use Measurestring in a Class. In a form I write cInt(Me.CreateGraphics.Measurestring("Text", mFont).Width) What do I write in a class (I dont't have the Me.CreateGraphics there...) ...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
62
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
2
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
7
by: teo | last post by:
Hallo, I'd like to retrieve the pixel length of a string. ------------------------ I decided to use MeasureString, but I have a problem with the graphic "instance" of it. I'm in a Sub and
6
by: Rainer Queck | last post by:
Hello NG, I am trying to adjust the column width of a DataGridView to the Text in its column header. To do this I am using the Graphics.MeasureString method. Unfortunatly I alway get a too...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.