Text height calculation questions

K

Keith G Hicks

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
 
K

Keith G Hicks

Well this is interesting. I found some info online
(http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.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
 
K

Keith G Hicks

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
 

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