DrawString() and lines measures

F

Fabio

Hi all,

I wonder if there is a way to get the bounds of each line drawn by the
Graphics.DrawString() considering that DrawString() can wordwrap the text,
so I cannot just consider the \n to identify the each line.

Any ideas?

Thanks!
 
V

VJ

What is your single line here.. how do you determine single line? or How do
you make drawstring, draw a string in a single line..? The answer depends
on the graphics object's width.. right?.. so your' graphics objects width ,
anything outside of the width will be wrapped.. right?

HTH
VJ
 
F

Fabio

VJ said:
What is your single line here.. how do you determine single line? or How
do you make drawstring, draw a string in a single line..? The answer
depends on the graphics object's width.. right?.. so your' graphics
objects width , anything outside of the width will be wrapped.. right?

I'll try to explain.

Take the text

"Hello to all the world.\nI'm here!"

Assume that the layout rectangle is not wide enough, the output will be
(with wordwrap enabled) something like

Hello to all
the world.
I'm here!

I need to get the bounds of each "Hello to all", "the world.", "I'm here!".

How?
 
M

Markus Stoeger

VJ said:
your answer is in your question... the width is the width of the Layout
rectangle...

He wants to get the width+height of _each_ single line, not just the
width of the whole thing. The line "Hello to all" has a bigger width
than "the world.".

I'm not sure how to solve this though. There'd have to be some way to
figure out where the line wrapper has inserted the line breaks. Then you
could split the text up at these positions and use MeasureString on each
item.

Max
 
F

Fabio

He wants to get the width+height of _each_ single line, not just the width
of the whole thing. The line "Hello to all" has a bigger width than "the
world.".

Ok, the witdth is not so important to me, so I can simply use something like
MeasureString() to get the maximum width.

For the Height it seems that the best way is the Font.GetHeight(Graphics),
fortunately the height returned is always the total height of a text line,
including the line spacing
(http://msdn.microsoft.com/library/d...us/usingtextandfonts/obtainingfontmetrics.asp)

Now I found that using the
PrintDocument.PrinterSettings.CreateMeasurementGraphics() sometimes it
return the wong size for MeasureString() wile printing, also if using the
same Graphics object on the PrintPreviewControl works file (e.g. printing
"Label" with Arial 8 in the Rectangle given by MeasureString() I got the
final "l" clipped... bizarre: all say that MeasureString() return a width
that is "oversized").

I wonder if there is a better way to measure text width.

Thanks again.
 
V

VJ

Ok Fabio... the graphics on Forms vs Graphics works different, You will
need to add a factor when working with direct Printer graphics, yes it is
not constant. It depends on printer margins.. I am not a expert in printer
or printer graphics...

Thanks
VJ
 
F

Fabio

Ok Fabio... the graphics on Forms vs Graphics works different, You will
need to add a factor when working with direct Printer graphics, yes it is
not constant. It depends on printer margins.. I am not a expert in printer
or printer graphics...

mmmmh... I don't understand.
In what way the margins would modify the measurement on printers?
It does not depend just on the Graphics.DpiX/Y of the device?

Thanks again.
 

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