Graphics Drawstring Justification

  • Thread starter Thread starter Bob Morris
  • Start date Start date
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
 
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.
 
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
 
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
 
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
 
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.
 
Back
Top