I need to convert text to image, using specified fonts, size, etc.
Looks like everything is ok when using standard DrawString method
but, when I'm trying to do it under Windows 7 I have my picture blurred
this is a screenshot from Windows XP / Windows 2008 server

and this is from Windows 7

also I tried to play with System.Drawing.Imaging.Encoder.Quality;
but even with max value of EncoderParameter blur effect doesn't disappear
I have no idea where is the problem, hope that you will be able to help.

Dani AI

Generated

This difference is not a bug in DrawString but a platform/rasterization mismatch. Windows versions and the two text stacks (GDI vs GDI+) rasterize fonts differently: ClearType/subpixel hinting, DPI, fractional positioning and the device context used all affect how crisp the pixels look. That explains why the same code can look sharp on XP and softer on Windows 7.

and are on the right track: switching rendering path and adjusting hinting help, but there are a few practical rules that reliably fix “soft” output:

  • Prefer the GDI-based renderer when you need screen-style ClearType output (TextRenderer) because it uses the OS text rasterizer; this often yields crisper text than DrawString. See the API docs for details: TextRenderer.DrawText.
  • Make sure the bitmap DPI and the Graphics DPI match the target display (use Bitmap.SetResolution). Mismatched DPI or fractional draw coordinates cause subpixel blur—snap text positions to integer pixels.
  • If you must use GDI+ (Graphics.DrawString), render at a larger size and downscale with high-quality interpolation, or draw the string as a vector (GraphicsPath) and fill it. These avoid the rasterizer differences.

Example pattern (draw at 2x then downscale):

/* draw at double size into a high-res bitmap, then downscale with high-quality interpolation */
Bitmap src = new Bitmap(w*2, h*2, PixelFormat.Format32bppPArgb);
src.SetResolution(192f, 192f);   // 2x 96dpi baseline
using (Graphics g = Graphics.FromImage(src))
{
    g.Clear(Color.Transparent);
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    TextRenderer.DrawText(g, text, new Font(family, size*2f), new Point(0,0), color, TextFormatFlags.NoPadding);
}
Bitmap final = new Bitmap(w, h, PixelFormat.Format32bppPArgb);
using (Graphics g = Graphics.FromImage(final))
{
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(src, 0, 0, w, h);
}

Notes: TextRenderer lives in System.Windows.Forms (may not be appropriate for some server environments). Try different fonts (hinting varies by family) and verify results on the target display. For background on font hinting/ClearType behavior see the ClearType overview: .

Recommended Answers

All 2 Replies

There are a couple things you can do:
1) Use TextRenderer.DrawText() and not Graphics.DrawString()
You did not specify if you were using .NET 2.0 or not, but if you are then you should use the TextRenderer class. The other method in the Graphics class does GDI which is not very good and outdated.
2) You can try and set the SmoothingMode to Antialiasing:

e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

use below option.
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.