469,299 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Graphics rendering question

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

Oct 18 '07 #1
5 3915
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
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;
}

Oct 18 '07 #2
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?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
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;
}

Oct 18 '07 #3
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.

Oct 18 '07 #4
On Oct 18, 11:16 am, not_a_commie <notacom...@gmail.comwrote:
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

Oct 25 '07 #5
Andy wrote:
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
Oct 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Dr. Zharkov | last post: by
5 posts views Thread by Tim | last post: by
6 posts views Thread by Chris Dunaway | last post: by
7 posts views Thread by dave | last post: by
11 posts views Thread by achiever | last post: by
12 posts views Thread by gsal | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.