473,395 Members | 1,443 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,395 software developers and data experts.

Text height calculation questions

I have the following code which runs fine

Imports System.Drawing

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim s as String = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Nulla magna. Nunc pede pede, scelerisque vel, luctus sed, eleifend a, enim.
Nulla at elit. Donec dictum feugiat risus. Pellentesque magna elit,
sollicitudin facilisis, tempus at, aliquam a, nibh. Duis tempus ante ac est
sollicitudin fringilla. Aenean dapibus. Suspendisse mi metus, mattis sed,
ullamcorper non, facilisis nec, sapien. Sed imperdiet. Vivamus cursus,
tellus a rhoncus pulvinar, orci felis tincidunt felis, a laoreet libero odio
sit amet dolor. Curabitur id dui nec enim placerat vehicula. Nullam tempor
odio sed nibh. Vivamus aliquet volutpat enim. Morbi rutrum, dui ut
consectetuer dictum, erat risus scelerisque nisl, scelerisque porta orci
magna ut quam. Phasellus egestas aliquet magna. In odio turpis, luctus at,
sodales non, iaculis nec, mauris. Sed porttitor purus ut quam. Maecenas quis
enim. In hac habitasse platea dictumst. Vestibulum eu quam eu sem
pellentesque dictum. Praesent et quam. Etiam dolor justo, venenatis at,
tempus a, pellentesque ut, leo. Etiam tellus ante, tempus non, posuere a,
dapibus in, leo. In facilisis ante vitae sapien. Aenean tincidunt arcu eget
leo. Fusce hendrerit lectus. Quisque dignissim quam sit amet lectus. Aliquam
at elit. Curabitur lorem enim, mattis ac, gravida ut, tempor in, lacus. In
volutpat laoreet turpis. Nulla ornare massa at neque. Suspendisse potenti.
Aliquam sapien turpis, imperdiet a, ullamcorper non, faucibus quis, erat.
Quisque adipiscing. Suspendisse accumsan, sapien quis pellentesque aliquam,
lectus risus imperdiet dui, ut sagittis urna massa non sapien. Aenean vitae
turpis id augue tincidunt vehicula. Cras sit amet nisi convallis orci
pretium tempus. Quisque et ligula eget neque faucibus placerat. Proin non
pede ac magna dignissim consequat. Nunc eros nibh, gravida eu, ultricies in,
condimentum vel, felis. Nunc vitae dui. Fusce scelerisque porta neque. Morbi
lobortis, justo nec consectetuer varius, risus diam ornare metus, quis
faucibus metus ante quis risus. Integer imperdiet velit. Vivamus suscipit
lectus. Donec adipiscing pulvinar erat. Sed lacinia iaculis lacus. Quisque
massa libero, tincidunt et, accumsan nec, iaculis vitae, tortor. Cras luctus
dolor at pede. Aliquam non nisi. Proin id arcu. Nam eget magna a urna
commodo commodo. Sed commodo. Donec non libero interdum dui ullamcorper
congue. Class aptent taciti sociosqu ad litora torquent per conubia nostra,
per inceptos himenaeos. Nullam et turpis. Nunc diam. Quisque ullamcorper mi
id nibh. Etiam in sem. Nulla fermentum eros vel risus. Nunc sit amet enim
sed enim molestie posuere. Pellentesque fringilla pretium ante. Integer mi.
Aliquam a massa. Aliquam erat volutpat. Nulla ultrices urna sed tellus.
Vestibulum in odio eu mi tristique condimentum. Integer et mi id odio congue
pulvinar. Phasellus quam ipsum, dictum a, ultricies et, tristique in, orci.
Sed nibh. Quisque imperdiet vulputate neque. Sed hendrerit dignissim pede.
Etiam vehicula imperdiet ipsum. Nunc consectetuer. Sed pulvinar. Aliquam
erat volutpat. Mauris vulputate pharetra neque. Sed tellus."

Dim factor As Double = 0.96 'Pixel/HundredsOfInch
Dim maxWidthInHundredsOfInch As Double = 192
Dim gr As Graphics = Graphics.FromHwnd(IntPtr.Zero)
Dim fnt As New Font("Arial", 7.5, FontStyle.Regular, GraphicsUnit.Point)
Dim size As SizeF = gr.MeasureString(s, fnt, maxWidthInHundredsOfInch *
factor)
Me.Label1.Text = (size.Height / factor).ToString()

End Sub

End Class
It doesn't calculate quite as I'd expect. If I take the same exact text and
put it in a Word doc and then set the margins, font and such (paper width
8.5"; left margin 1"; right margin 5.58"; Arial 7.5 normal single spaced) to
the above I get a height of about 9.6 inches. But using the code above it
comes out 10.13 inches. That's off by 0.53 inches. Too much for me to
ignore.

The line Dim factor As Double = 0.96 'Pixel/HundredsOfInch came from someone
else so I'm not sure why the 0.96 factor is needed. If anyone can answer
that I'd appreciate it (the person that gave that to me is not available).
Everything else seems to make sense so I'm not clear as to why I'd be
getting such a large difference. It's not critical that it be exact but it
should be very close.

Also, at one time, some folks in another post of mine said that this could
be quite driver and printer dependent. It seems to me that the code above
would not be concerned with any of that. Am I missing something? Is the
graphics class just assuming the default Windows printer driver?

Are there other settings for the font and/or the gr object that I need to
set that would affect this?

Any help would be appreciated.

Thank you,

Keith
Jul 24 '08 #1
2 1440
Well this is interesting. I found some info online
(http://www.tech-archive.net/Archive/...otnet.framewor
k.drawing/2004-06/0327.html) about MeasureString that made it clear that I
should add StringFormat.GenericTypographic to the parameters as follows:
Dim size As SizeF = gr.MeasureString(s, fnt, maxWidthInHundredsOfInch *
factor, StringFormat.GenericTypographic)

That seems to have helped a lot. I'm still trying to figure out the
antialias part but I have a feelign that isn't an issue for me. We'll see.

I also tried setting my default printer in Windows to various drivers that I
have set up for various clients. Each time I changed it I reran the code
below and got the exact same results every time. So I'm not sure that the
default driver has anything at all to do with the calcluation. It would be
nice if that were the case. Could anyone confirm this?

Thanks,

Keith

"Keith G Hicks" <kr*@comcast.netwrote in message
news:OH**************@TK2MSFTNGP03.phx.gbl...
I have the following code which runs fine

Imports System.Drawing

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

Dim s as String = "Lorem ipsum dolor sit amet, consectetuer adipiscing
elit.
Nulla magna. Nunc pede pede, scelerisque vel, luctus sed, eleifend a,
enim.
Nulla at elit. Donec dictum feugiat risus. Pellentesque magna elit,
sollicitudin facilisis, tempus at, aliquam a, nibh. Duis tempus ante ac
est
sollicitudin fringilla. Aenean dapibus. Suspendisse mi metus, mattis sed,
ullamcorper non, facilisis nec, sapien. Sed imperdiet. Vivamus cursus,
tellus a rhoncus pulvinar, orci felis tincidunt felis, a laoreet libero
odio
sit amet dolor. Curabitur id dui nec enim placerat vehicula. Nullam tempor
odio sed nibh. Vivamus aliquet volutpat enim. Morbi rutrum, dui ut
consectetuer dictum, erat risus scelerisque nisl, scelerisque porta orci
magna ut quam. Phasellus egestas aliquet magna. In odio turpis, luctus at,
sodales non, iaculis nec, mauris. Sed porttitor purus ut quam. Maecenas
quis
enim. In hac habitasse platea dictumst. Vestibulum eu quam eu sem
pellentesque dictum. Praesent et quam. Etiam dolor justo, venenatis at,
tempus a, pellentesque ut, leo. Etiam tellus ante, tempus non, posuere a,
dapibus in, leo. In facilisis ante vitae sapien. Aenean tincidunt arcu
eget
leo. Fusce hendrerit lectus. Quisque dignissim quam sit amet lectus.
Aliquam
at elit. Curabitur lorem enim, mattis ac, gravida ut, tempor in, lacus. In
volutpat laoreet turpis. Nulla ornare massa at neque. Suspendisse potenti.
Aliquam sapien turpis, imperdiet a, ullamcorper non, faucibus quis, erat.
Quisque adipiscing. Suspendisse accumsan, sapien quis pellentesque
aliquam,
lectus risus imperdiet dui, ut sagittis urna massa non sapien. Aenean
vitae
turpis id augue tincidunt vehicula. Cras sit amet nisi convallis orci
pretium tempus. Quisque et ligula eget neque faucibus placerat. Proin non
pede ac magna dignissim consequat. Nunc eros nibh, gravida eu, ultricies
in,
condimentum vel, felis. Nunc vitae dui. Fusce scelerisque porta neque.
Morbi
lobortis, justo nec consectetuer varius, risus diam ornare metus, quis
faucibus metus ante quis risus. Integer imperdiet velit. Vivamus suscipit
lectus. Donec adipiscing pulvinar erat. Sed lacinia iaculis lacus. Quisque
massa libero, tincidunt et, accumsan nec, iaculis vitae, tortor. Cras
luctus
dolor at pede. Aliquam non nisi. Proin id arcu. Nam eget magna a urna
commodo commodo. Sed commodo. Donec non libero interdum dui ullamcorper
congue. Class aptent taciti sociosqu ad litora torquent per conubia
nostra,
per inceptos himenaeos. Nullam et turpis. Nunc diam. Quisque ullamcorper
mi
id nibh. Etiam in sem. Nulla fermentum eros vel risus. Nunc sit amet enim
sed enim molestie posuere. Pellentesque fringilla pretium ante. Integer
mi.
Aliquam a massa. Aliquam erat volutpat. Nulla ultrices urna sed tellus.
Vestibulum in odio eu mi tristique condimentum. Integer et mi id odio
congue
pulvinar. Phasellus quam ipsum, dictum a, ultricies et, tristique in,
orci.
Sed nibh. Quisque imperdiet vulputate neque. Sed hendrerit dignissim pede.
Etiam vehicula imperdiet ipsum. Nunc consectetuer. Sed pulvinar. Aliquam
erat volutpat. Mauris vulputate pharetra neque. Sed tellus."

Dim factor As Double = 0.96 'Pixel/HundredsOfInch
Dim maxWidthInHundredsOfInch As Double = 192
Dim gr As Graphics = Graphics.FromHwnd(IntPtr.Zero)
Dim fnt As New Font("Arial", 7.5, FontStyle.Regular, GraphicsUnit.Point)
Dim size As SizeF = gr.MeasureString(s, fnt, maxWidthInHundredsOfInch *
factor)
Me.Label1.Text = (size.Height / factor).ToString()

End Sub

End Class
It doesn't calculate quite as I'd expect. If I take the same exact text
and
put it in a Word doc and then set the margins, font and such (paper width
8.5"; left margin 1"; right margin 5.58"; Arial 7.5 normal single spaced)
to
the above I get a height of about 9.6 inches. But using the code above it
comes out 10.13 inches. That's off by 0.53 inches. Too much for me to
ignore.

The line Dim factor As Double = 0.96 'Pixel/HundredsOfInch came from
someone
else so I'm not sure why the 0.96 factor is needed. If anyone can answer
that I'd appreciate it (the person that gave that to me is not available).
Everything else seems to make sense so I'm not clear as to why I'd be
getting such a large difference. It's not critical that it be exact but it
should be very close.

Also, at one time, some folks in another post of mine said that this could
be quite driver and printer dependent. It seems to me that the code above
would not be concerned with any of that. Am I missing something? Is the
graphics class just assuming the default Windows printer driver?

Are there other settings for the font and/or the gr object that I need to
set that would affect this?

Any help would be appreciated.

Thank you,

Keith



Jul 25 '08 #2
I'm not displaying any text in this process. I'm simply calculating height
for billing purposes. It appears from the info I found on MSDN that
antialiasing settings should not affect the actual height calculation. If
anyone knows if this is true or not, please let me know.

Keith

"Keith G Hicks" <kr*@comcast.netwrote in message
news:uM**************@TK2MSFTNGP06.phx.gbl...
Well this is interesting. I found some info online
(http://www.tech-archive.net/Archive/...otnet.framewor
k.drawing/2004-06/0327.html) about MeasureString that made it clear that I
should add StringFormat.GenericTypographic to the parameters as follows:
Dim size As SizeF = gr.MeasureString(s, fnt, maxWidthInHundredsOfInch *
factor, StringFormat.GenericTypographic)

That seems to have helped a lot. I'm still trying to figure out the
antialias part but I have a feelign that isn't an issue for me. We'll see.

I also tried setting my default printer in Windows to various drivers that
I
have set up for various clients. Each time I changed it I reran the code
below and got the exact same results every time. So I'm not sure that the
default driver has anything at all to do with the calcluation. It would be
nice if that were the case. Could anyone confirm this?

Thanks,

Keith

Jul 25 '08 #3

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

Similar topics

4
by: Philip K | last post by:
I write software that needs to draw simple engineering drawings for factory workers to read. A standard for drawings is to be able to read the text from the bottom right corner. The GDI...
2
by: gjtired | last post by:
Hi, I'm not sure the best way to do something and I hope someone can help me. I'm developing a web application that a user enters test questions in. I store the questions in a database. When a...
2
by: Zanna | last post by:
Hi all! I'm in difficulty with this: I need to know the height of a text line that is written with Graphics.DrawString(). In theory I need just to get the Graphics.MeasureString() result. But...
10
by: liups | last post by:
Hi, I have a multi-line textbox that accepts text input, I want it to adjust its height according to the text it holds, so I use a SendMessage API to get the line count of the text, then set the...
1
by: comp.lang.php | last post by:
Sorry folks, I'm on a roll!!! I need to calculate the height or width of an image if you change either the width or height and don't change BOTH of them at the same time, this is to "vector" the...
2
by: RateTheBuilder | last post by:
Hi I have a multi-line text box displaying data inside a datagrid. I have the height set to 50px and the width to 200px. If the data populating the text box is larger than this, then naturally...
5
by: OtisUsenet | last post by:
Hi, I'm killing myself trying to get a "horizontal nav bar" where some text is left aligned, and some is aligned to the right, kind of like this: LEFT ...
14
by: David C. Stone | last post by:
Is there a way in CSS to adjust the baseline position of text marked up with either <sup></supor <sub></sub>, relative to the surrounding text? Reason for asking is that the default rendering in...
12
by: Keith G Hicks | last post by:
I have a client that I wrote an MS Access app for quite a few years ago where they need to know the height in inches of a block of text for billing their customers. The MS Access app opens Word in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.