Hi,
I am using the following code to render a text string in a new bitmap
file. The code works, but the text looks, well, crappy, even though I
told it to use ClearType hints. Any idea how to make the text look
nicer?
Thanks
Andy
internal static string RenderDate( DateTime date ) {
Bitmap bitmap;
string result, formattedDate;
Font font;
Graphics canvas;
SizeF textSize;
SolidBrush brush;
result = Path.Combine(
Path.GetTempPath(),
date.ToString( "ddMMyyyy" )
);
result = Path.ChangeExtension( result, ".bmp" );
formattedDate = date.ToString( "d" );
using ( font =
new Font( FontFamily.GenericSansSerif, 12,
FontStyle.Regular )
) {
using ( bitmap = new Bitmap( 1, 1 ) ) {
using ( canvas = Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
textSize =
canvas.MeasureString( formattedDate, font );
}
}
using ( brush = new SolidBrush( Color.Black ) ) {
using ( bitmap = new Bitmap(
Convert.ToInt32( System.Math.Ceiling( textSize.Width ) ),
Convert.ToInt32( System.Math.Ceiling( textSize.Height ) )
) ) {
using ( canvas =
Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
canvas.DrawString(
formattedDate,
font,
brush,
new PointF( 0, 0 )
);
}
//
bitmap.RotateFlip( RotateFlipType.Rotate270FlipNone );
if ( File.Exists( result ) ) {
File.Delete( result );
}
bitmap.Save( result );
}
}
}
return result;
}
Nicholas Paldino [.NET/C# MVP] - 18 Oct 2007 16:02 GMT
Andy,
Well, the standard sans serif font, as far as I know, is not a ClearType
font. Have you tried looking at any of the other TextRenderingHint values?
like AntiAlias or AntiAliasGridFit?

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi,
>
[quoted text clipped - 67 lines]
> return result;
> }
Nicholas Paldino [.NET/C# MVP] - 18 Oct 2007 16:10 GMT
Andy,
Well, the standard sans serif font, as far as I know, is not a ClearType
font. Have you tried looking at any of the other TextRenderingHint values
like AntiAlias or AntiAliasGridFit?

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi,
>
[quoted text clipped - 67 lines]
> return result;
> }
not_a_commie - 18 Oct 2007 16:16 GMT
The good Cleartype font is Segoe UI. Also, It's not clear from the
code there what color depth of graphics object you have. You will need
32 bit coloring (aka, something with an alpha channel) for the
Cleartype to work.
Andy - 25 Oct 2007 19:32 GMT
> The good Cleartype font is Segoe UI. Also, It's not clear from the
> code there what color depth of graphics object you have. You will need
> 32 bit coloring (aka, something with an alpha channel) for the
> Cleartype to work.
Hi, I actually got this to work, sorry for not following up sooner.
Instead of saving a bitmap, I saved as a jpg at 99% quality and that
cleared the problem right up. Which is weird, because I thought a
bitmap would be better quality (I was using 32bit with Alpha
bitmaps... the code was creating a new Bitmap instance from scratch).
Andy
Peter Duniho - 25 Oct 2007 19:45 GMT
> Hi, I actually got this to work, sorry for not following up sooner.
> Instead of saving a bitmap, I saved as a jpg at 99% quality and that
> cleared the problem right up. Which is weird, because I thought a
> bitmap would be better quality (I was using 32bit with Alpha
> bitmaps... the code was creating a new Bitmap instance from scratch).
Assuming the same bitmap dimensions, an image saved as .bmp (or other
lossless format, such as .png or .tiff) _will_ be better quality than a
.jpeg file.
So, it's practically certain that there's more to the difference in your
code than simply changing the file format.
Pete