A
Andy
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;
}
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;
}