Graphics Drawstring Justification

B

Bob Morris

I am using the DrawString graphics method to print out the address lines on
a letterhead. These have to be right justified but the justification that I
achieve by any method that I have tried still gives a very ragged result.
Over 12 lines of text in 9 point Times New Roman can have some lines as much
as one em out at the right margin. I have tried justifying using
StringFormat, and also by using MeasureString and deducting the result from
the right margin to get the starting point for the Drawstring call. Both
give the same result.

Please tell me this is curable!

Bob
 
A

Anders

Have you tried the characterRange-way of doing things?
Specify the font variable and some rectangle rect to draw
the text in (big enough not to wrap).
Then to the following

Region[] charRegions = new Region[1];
String str = "Foo Bar";
CharacterRange[] charRanges = { new CharacterRange(0,
str.Length) };
StringFormat strFormat = new StringFormat();
strFormat.SetMeasurableCharacterRanges(charRanges);
charRegions = g.MeasureCharacterRanges(str, font, rect,
strFormat);
Rectangle strBounds = charRegions[0].GetBounds(graphics);

Now strBounds .Width and .Height contains the size of the
string. I think this could be more accurate than the
MeasureString in some cases.
 
B

Bob Powell [MVP]

You should get a great retult if you specify a StringFormat and set the
alignment to 'Far'

In all cases you should base your StringFormat on the GenericTypographic
setting by using...

StringFormat sf=(StringFormat)StringFormat.GenericTypographic.Clone();

Just using GenericTypographic raw will result in you permanently changing it
for the lifetime of your app.

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

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

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

Bob Powell [MVP]

You should get a great result if you specify a StringFormat and set the
alignment to 'Far'

In all cases you should base your StringFormat on the GenericTypographic
setting by using...

StringFormat sf=(StringFormat)StringFormat.GenericTypographic.Clone();

Just using GenericTypographic raw will result in you permanently changing it
for the lifetime of your app.

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

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

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

Bob Morris

Thanks Gentlemen,

Both approaches gave much better results than I was getting previously, with
the best seeming to be the Generic Typographic option. Strangely this latter
makes the justification look even worse in the print preview but is fine
when printed out. What price WSIWYG?

Best Regards
Bob Morris
 
J

Joe White

If you can make your preview draw text using TextRenderingHint.AntiAlias
(together with GenericTypographic), then everything should line up, though
the text will look "grayer" on the screen, i.e., lower-contrast.
 

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