Graphics.drawString() looks terrible

I have SmoothingMode set to AntiAlias... all the lines and polygons come out nice and smooth, but the text is not antialiased, it looks terrible.

I'm creating a new bitmap, and getting the graphics object off that.

I have noticed that if you load a .gif into your bitmap, and then drawstring on top of that you get much better results.

Anyone know how I can get smooth text without loading a gif first?

thanks

[418 byte] By [codefund.com] at [2007-12-16]
# 1
Can you post a small sample of the code, to demonstrate what you're doing? I haven't seen this behavior at all, but perhaps I've not tried what you're doing.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
basically this:

Bitmap bmp = new Bitmap(ImgWidth, ImgHeight);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawString("x100", myFont, fontPen.Brush, GraphicMargin, GraphicMargin);
pictureBox1.Image = bmp;

and here is a screen capture, and magnification, of what it looks like:

http://www.ol0lo.com/drawString.html

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
have you tried NOT setting the SmoothingMode to AntiAlias

not sure if that'll do it for you, but antialias is for smoothing out edges and what not and with text, it's usually better to have hard edges

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
I have to agree with Erik on this one. I tried it both ways, and they look OK to me, but without anti-aliasing looks better for large fonts like this.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
If you want to turn on AntiAliasing for text, you have to do so separately with the TextRenderingHint property of the Graphics class.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6
the other thing that you might attempt is setting the text rendering hint..if it's an XP machine or a machine that supports ClearType, to get the best quality use:

Graphics g;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit

there's also an AntiAlias and a few others...but that might be one way to help.
Hope that helps some..
-Randy

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7
I had the same problem...

When creating the Bitmap I was specifying the PixelFormat to be PixelFormat.Format32bppPArgb

And when viewing the output via the DrawString() itlooked very similar to your output. I then noticed that my notebook's video card didn't support a color depth of 32bpp. So I changed the PixelFormat to PixelFormat.Format24bppRgb (which my video card supports) and everything looks great.

So try changing
Bitmap bmp = new Bitmap(ImgWidth, ImgHeight); -->
Bitmap bmp = new Bitmap(ImgWidth, ImgHeight, PixelFormat.[Whatever Color Depth you Video Card Supports]);

BTW, does anybody know how to get the Color Depth the system is set to?

Good Luck
Chris

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8
Interesting. I thought the color depth was in the SystemInformation class somewhere, but didn't find it. It may be buried somewhere, and I'm sure someone will point it out, if it's not.

But when it comes to determining information about your hardware, if you can't find it in the .NET Framework, your next step is to look in WMI (Windows Management Instrumentation), handled by the System.Management namespace. Here's code you can use to retrieve the color depth of all installed video controllers:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mc As New ManagementClass("Win32_VideoController")
Dim mo As ManagementObject
For Each mo In mc.GetInstances()
Debug.WriteLine(mo("CurrentBitsPerPixel"))
Next
End Sub

You'll need to set a reference to the System.Management assembly, and I added

Imports System.Management

to the top of my module. There may be an easier solution, but this worked for me. You'll be amazed at what's available through WMI, if you need stuff that's not in the .NET Framework explicitly.

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9
yah, i looked around and couldn't find it either, figuring it would be in there, but no cigar :(

thanx for that piece of code! I was always amazed by what WMI could do and amazed how extremely not documented and not intuitive WMI was, but then again I haven't messed with it much in .NET, maybe it's easier and I should shut my mouth...thanx again for the code :)

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10
MAC addresses of lan cards is a good example of useful stuff available through WMI :)
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11
Or amount of space on a drive, free or total. This is what got me started down the WMI path -- I refused to call the Win32 API for this sort of thing. I'm always amazed at what I find in the WMI docs when I start digging.
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms General...