473,320 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Strange behavior of DrawString

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 =
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiii";
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
Nov 16 '05 #1
6 9360
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
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
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 =
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiii";
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

Nov 16 '05 #2
There is a good article on windowsforms.net that explains this...

http://windowsforms.net/articles/gdiptext.aspx

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

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.

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
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 =
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiii";
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

Nov 16 '05 #3
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
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")

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

What's the secret?

TIA.
Sam

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
There is a good article on windowsforms.net that explains this...

http://windowsforms.net/articles/gdiptext.aspx

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

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.

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:eI**************@TK2MSFTNGP15.phx.gbl...
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 =
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiii";
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


Nov 16 '05 #4
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...)
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote:
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
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii")

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

What's the secret?

Nov 16 '05 #5
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
Nov 16 '05 #6
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.
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/

"Sam Kong" wrote:
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.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: David Rifkind | last post by:
I've seen some strange string formatting behavior on several XP and 2000 machines. I can get the same results with Graphics.DrawString, but the easiest way to see it is to put a label on a VB or...
1
by: timtos | last post by:
I am working on a custom treeView and have some problems with a) node.Bounds.Width b) concatenate strings drawn with DrawString(...) a) I want to highlight a selected node and used the...
1
by: David Lindgren | last post by:
Hello! I am using the DrawString method with different StringAlignments passed to it and the result varies alot! Take a look at this screenshot:...
1
by: Drew | last post by:
I posted this to framework.drawing, but did not get any responses... Maybe here?? I am drawing some text using DrawString() and using a bounding rectangle so that when the string is too long it...
2
by: Piedro | last post by:
Can someone reproduce the following error? I'm using the module at the bottom of my post to owner draw a menu items, I call the module from a form like this: Private Sub mnuOpen_DrawItem(ByVal...
1
by: Joao | last post by:
Hi, I'm trully excited to use VB.NET's PrintDocument to make my printouts. On them I draw strings, images and lines. All perfect, if I invoke the Print or the PrintPreview one time per run. At...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
2
by: ChrisNightingale | last post by:
Hi everybody, I have an odd issue which I'm not sure how to resolve. I'm basically implementing a print mechanism which takes a series of controls and reproduces them on a print document. So...
2
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.