Groups | Blog | Home
all groups > dotnet drawing api > august 2006 >

dotnet drawing api : DrawString problem


Fabio
8/30/2006 12:22:55 PM
Hi all!

I have a problem with the DrawString method.

I wrote this little code that reproduce the problem:

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

StringFormatFlags flags = StringFormatFlags.MeasureTrailingSpaces;

Rectangle r = new Rectangle(10, 10, 100, 100);

StringFormat sf = new StringFormat(flags);

sf.Alignment = StringAlignment.Far;

g.DrawString("This is\na string\non more lines",

this.Font, Brushes.Blue, r, sf);

g.DrawRectangle(Pens.Red, r);

}


The problem is: why if I set the MeasureTrealingSpaces flag the text is not
aligned correctly to the right?
The text in this sample does not have trailing spaces!
I know I can remove that flag, but if a single line has trailing spaces
(note that the last line is drawn correctly!) I want that them are drawn.

Thanks for the help :)

--

Free .Net Reporting Tool - http://www.neodatatype.net

Bob Powell [MVP]
8/31/2006 8:56:04 PM
The MeasureTrailingSpaces format flag is used by MeasureString.

The poor fit of text in a rectangle is due to the inaccurate way GDI+
renders text to speed up output. If you slow things down you can get a
pretty good rendering.

Here is an altered version that renders as best GDI+ can...

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

g.TextRenderingHint = TextRenderingHint.AntiAlias;

StringFormatFlags flags = StringFormatFlags.FitBlackBox;

Rectangle r = new Rectangle(10, 10, 100, 100);

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

sf.FormatFlags |= flags;

sf.Alignment = StringAlignment.Far;

g.DrawString("This is\na string\non more lines",

this.Font, Brushes.Blue, r, sf);

g.DrawRectangle(Pens.Red, r);

}



Note the use of the GenericTypographic format and the text rendering hint
set to it's most accurate setting.


--
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]

Fabio
8/31/2006 9:34:54 PM

"Bob Powell [MVP]" <bob@_spamkiller_.bobpowell.net> ha scritto nel messaggio
news:OKWFg8SzGHA.1252@TK2MSFTNGP04.phx.gbl...


[quoted text, click to view]

mmm... thanks for the replay.
Maybe I didn't understand the meaning of MeasureTrailingSpaces, but I'd like
that if a line ends with a space this space will be drawn, but the
MeasureTrailingSpaces add some spaces also to trimmed lines.


Bob Powell [MVP]
9/1/2006 12:00:00 AM
You're right. It's a bug. Don't expect a fix anytime soon because all
efforts are going into WPF and GDI+ is on the way out.



Graphics g = e.Graphics;

g.TextRenderingHint = TextRenderingHint.AntiAlias;

StringFormatFlags flags = StringFormatFlags.FitBlackBox |
StringFormatFlags.MeasureTrailingSpaces;

Rectangle r = new Rectangle(10, 10, 100, 100);

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

sf.FormatFlags |= flags;

sf.Alignment = StringAlignment.Far;

g.DrawString("This is\na string \non more lines",

this.Font, Brushes.Blue, r, sf);

g.DrawRectangle(Pens.Red, r);


--
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]

Fabio
9/1/2006 12:00:00 AM
"Bob Powell [MVP]" <bob@_spamkiller_.bobpowell.net> ha scritto nel messaggio
news:uYpEMAZzGHA.2036@TK2MSFTNGP05.phx.gbl...

[quoted text, click to view]

Whaaat??? :)

And WPF will not use GDI+?

Bob Powell [MVP]
9/5/2006 8:15:40 PM
You can use GDI+ but there will be no improvements to GDI+ because WPF
already does more than GDI+ ever did.

--
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]

Randolpho
9/19/2006 9:00:43 AM

[quoted text, click to view]

No, WPF uses DirectX.
Fabio Z
9/25/2006 12:00:00 AM
"Randolpho" <randolpho@gmail.com> ha scritto nel messaggio

[quoted text, click to view]

Ok, but the wrapper layer?
I don't think they want me to use DX directly :)


Randolpho
9/25/2006 10:50:16 AM

[quoted text, click to view]

WPF doesn't expose DirectX, it *uses* DirectX.

But you could still use DirectX if you wanted to, either using Managed
DirectX or native DirectX and C++/CLI.
Fabio Z
9/26/2006 12:00:00 AM
"Randolpho" <randolpho@gmail.com> ha scritto nel messaggio


[quoted text, click to view]

Yes, ok, but my question is: if I want to draw a line I have to use DX if I
don't what to use GDI+ that is obsolete?

Randolpho
9/26/2006 7:39:52 AM

[quoted text, click to view]

I haven't read any official documentation that states that GDI+ is
fully deprecated and obsolete; it's just on the way out in favor of a
new paradigm.

Still, you don't use DirectX *yourself* to draw in WPF, you use the WPF
Shape class and other utility classes that derive from it -- such as
the Line class and the Ellipse class -- to draw in WPF. They, in turn,
use DirectX *for* you. Check out the following:

http://windowssdk.msdn.microsoft.com/en-us/library/ms747393.aspx
(Shapes and Basic Drawing in Windows Presentation Foundation)
Fabio Z
9/27/2006 12:00:00 AM
"Randolpho" <randolpho@gmail.com> ha scritto nel messaggio
[quoted text, click to view]

Ah! Ok, that's what I needed to know.

Thanks

AddThis Social Bookmark Button