473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text height calculation questions

I have the following code which runs fine

Imports System.Drawing

Partial Class _Default

Inherits System.Web.UI.P age

Protected Sub Button2_Click(B yVal sender As Object, ByVal e As
System.EventArg s) 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 maxWidthInHundr edsOfInch As Double = 192
Dim gr As Graphics = Graphics.FromHw nd(IntPtr.Zero)
Dim fnt As New Font("Arial", 7.5, FontStyle.Regul ar, GraphicsUnit.Po int)
Dim size As SizeF = gr.MeasureStrin g(s, fnt, maxWidthInHundr edsOfInch *
factor)
Me.Label1.Text = (size.Height / factor).ToStrin g()

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 1451
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.Ge nericTypographi c to the parameters as follows:
Dim size As SizeF = gr.MeasureStrin g(s, fnt, maxWidthInHundr edsOfInch *
factor, StringFormat.Ge nericTypographi c)

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.ne twrote in message
news:OH******** ******@TK2MSFTN GP03.phx.gbl...
I have the following code which runs fine

Imports System.Drawing

Partial Class _Default

Inherits System.Web.UI.P age

Protected Sub Button2_Click(B yVal sender As Object, ByVal e As
System.EventArg s) 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 maxWidthInHundr edsOfInch As Double = 192
Dim gr As Graphics = Graphics.FromHw nd(IntPtr.Zero)
Dim fnt As New Font("Arial", 7.5, FontStyle.Regul ar, GraphicsUnit.Po int)
Dim size As SizeF = gr.MeasureStrin g(s, fnt, maxWidthInHundr edsOfInch *
factor)
Me.Label1.Text = (size.Height / factor).ToStrin g()

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.ne twrote in message
news:uM******** ******@TK2MSFTN GP06.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.Ge nericTypographi c to the parameters as follows:
Dim size As SizeF = gr.MeasureStrin g(s, fnt, maxWidthInHundr edsOfInch *
factor, StringFormat.Ge nericTypographi c)

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
526
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 defaults vertical text to be readable from the left side of the page (upside down). It seems from the news group that many people are having problems with this. Additionally we have the need to be able to draw text along an angled line,
2
1515
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 person takes the test I need to be able to display the questions. I can use a multiline text box but I want the user to see the entire question and not have to use the scroll bars. I also don't want the scroll bars visible if they are not used. I...
2
5383
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 this works if the string is on a single line. If the string goes on more lines (caused by a new line or word wrap), it seems that I need to compute the space between the lines, since the height
10
2554
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 TextBox.Height to (linecount * font.Height), but there is something wrong with this calculation, the textbox is always larger than the text lines, any ideas? Thank you.
1
3892
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 image. Here is the class method, it obviously doesn't work: /**
2
1951
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 the scrollbars appear. What I need is rather than the vertical scroll bar to appear, that the text box increases in height to contain the text. Any ideas or pointers to tutorials would be greatly appreciated.
5
10159
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 RIGHT -------------------------------------------------------------- This is what I'm trying:
14
37098
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 most browsers always increases the spacing between the line of text containing these elements and those above/below that lack it. I can easily reduce the size of the subscript or superscript
12
1838
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 the background, makes some settings (font, font size, margins, etc.) and gets the height. It's all worked quite well for 3 or 4 years. However, the whole thing is going to be redone for use in browsers in asp.net with vb.net. We want to...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.