Strange behavior of DrawString

S

Sam Sungshik Kong

Hello!

I'm testing Graphics.DrawString and it's very strange.

I created an event handler for the form.s Paint event.
(There's no other code in the form.)

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
string str =
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
using (SolidBrush b = new SolidBrush(Color.Black))
{
using (Font f = new Font("Microsoft Sans Serif", 8.25f))
{
g.DrawString(str, f, b, 10.0f, 10.0f);
}
}
}

Actually it draws like the following kind(I can't show it in text):

iiiiiiiiiiiiiiiiiiiiiiiiiiiii i i i i i i i i i i i i i i i i i i i i i i i
i i i i i i i i i i i

Can anybody explain why it's like that?


TIA.
Sam
 
S

Stefan Simek

It's because of the way GDI+ renders text. The aim was to have text
rendering functions that would render the text with the same proportions
regardless of the scaling. With small DPI, though, it results in characters
being moved in a strange way. To see what exactly is going on, try setting
the Graphics.TextRenderingHint to the following values:

TextRenderingHint.AntiAlias - You lose readability for small font
sizes, but this is the exact representation how the text is meant to look
like. Note that two characters may not look the same in different positions.

TextRenderingHint.AntiAliasGridFit - This is the default (unless you
have ClearType turned on). The characters look always the same (start of
every character is aligned to the displaying device's grid), and the
character kerning is adjusted so that the size of the string is roughly the
same as with the previous. This is what you see when rendering the
iiiiiiiiiiiiiiii string. Because each character starts at an approximate
position to match the display grid, the space between characters may vary.

Also, for much more information on GDI+ see Bob Powell's GDI+ FAQ at
http://www.bobpowell.net/gdiplus_faq.htm

HTH,
Stefan
 
B

Bob Powell [MVP]

S

Sam Sungshik Kong

Thanks Bob and Stefan for the replies.

Now I understand why that happened.
I have an additional question.

I tried every TextRenderingHint enum.
However, the result is either blurry text or variable width text.
(I tested with
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")

This is not the case with TextBox.
It's neither blurry nor variable-width.

What's the secret?

TIA.
Sam
 
I

Ian Griffiths [C# MVP]

The secret is that the TextBox ignores the specified character width. It
chooses to draw the characters at a width that is different from what the
font metrics say it should be to avoid this problem.

And it does this by using GDI to draw the text rather than GDI+. (The
TextBox class is just a wrapper around the Win32 textbox control, so none of
the rendering is done by .NET for that particular control.)

The only way I know of to get the same results in .NET is to use interop to
call the GDI drawing APIs rather than using GDI+.

Personally I prefer anti-aliased text though. You called it 'blurry' but I
find it infinitely more legible than the default. (But that's probably
because my flat panel screen has a resolution of 150 pixels per inch, so the
blur is on a small enough scale not to be visible...)
 
S

Sam Kong

Thanks. You cleared things up. Now I understand it.
because my flat panel screen has a resolution of 150 pixels per inch, so the
blur is on a small enough scale not to be visible...)

You use 150 DPI?
That's surprising.
Some applications look broken in other DPI than 96 according to my experience.


Sam
 
I

Ian Griffiths [C# MVP]

I don't have Windows set to 150dpi. I have it set to 96dpi. But the
*physical* resolution of my screen is 150dpi.

This means everything is tiny, but I like it that way - I can get lots onto
the screen. And the point I was trying to make is that with a sufficiently
high resolution screen, anti-aliasing and cleartype don't look blurry, they
just look much much better than the 1bpp rendered text.
 

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