Groups | Blog | Home
all groups > dotnet drawing api > october 2007 >

dotnet drawing api : Printing multiline text, how to get the correct rectangle height.


Marius Horak
10/17/2007 8:22:55 AM
I'm using

float height;
MeasureString(text, font, size, new StringFormat(), out charCount, out
lineCount);

to get number of lines, font is Ariel 9pt.

Next

height = lineCount * font.Height;

to get rectangle height.


But the height is not big enough. It's OK for 3-4 line but later the
bottom of the text is being cut off.

So I modified to

height = lineCount * font.Height + lineCount / 2 + 1;

But now the height is a bit too big.

Any idea how to get the correct height?

Thanks

Marius Horak
10/17/2007 1:37:03 PM
[quoted text, click to view]

If I understand you correctly printing would be like this:

1. Get number of characters per line (N).
2. Print the first N charactes as a line.
3. Remove printed characters from the text.
4. All printed? If no, advance Y by the font height and go to 1.

This is insane.
People resposible for this should be sacked.

MH
Bob Powell [MVP]
10/17/2007 8:04:10 PM
Unfortunately this is an iterative process in which you will modify the
font size and then use MeasureString to determine the size of the text
rectangle until such times as the rectangle fits the area (more or less)


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
Marius Horak
10/17/2007 11:56:34 PM
God bless you Doug Forster.

Doug Forster
10/18/2007 12:00:00 AM
Hi Marius,

I think you have misinterpreted Bob's answer. He was telling you how to
search for a font height to fit a given rectangle. I suspect this is not
what you are trying to do.
You don't seem to be inspecting the SizeF return result from your call to
MeasureString.

Cheers
Doug Forster

[quoted text, click to view]

Phil H
10/21/2007 6:23:21 AM
On 17 Oct, 19:04, "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net>
[quoted text, click to view]

I don't think it needs to be trial and error. The SizeF returned by
the MeasureString method reports on the space occupied by the given
text using the given font. That space is proportiuonal to font height,
so for example if SizeF comes back twice as large as the space
available then the font height has to be readjusted to half the height
of the original font.

I know because I have used this strategy successfully.
Bob Powell [MVP]
10/22/2007 12:00:00 AM
Your strategy doesn't take into account the fact that the font width,
line wrapping, kerning and justification all change with font height.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]
Marius Horak
10/22/2007 3:42:23 AM
[quoted text, click to view]

I'm not printing RTF. All text has the same font.


AddThis Social Bookmark Button