Graphics rendering question

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;
}
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
N

not_a_commie

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.
 
A

Andy

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
 
P

Peter Duniho

Andy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top