.NETGURU
best quality text rendering?
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Kirk Marple (VIP)
i just wrote ths neat-o command-line image generation tool for creating graphics for my ASP.NET app, and i'm trying to get the best quality text out of it. (btw, i'd be happy to give anybody the C# project, if they're interested.)

the docs inside VS.NET are pretty slim, and i just want to make sure i'm doing the right things to get the best quality anti-aliased text out. if anybody knows GDI+ well, can they take a look at this code and see if it looks correct?

see http://www.1968.org/images/asset_format_txt.gif for an example image that this code created. (using Impact 10pt, not bold, not centered, 100x20, fore: #9A0000, back: #FFFFFF)

thanks!
Kirk

---------------------

here's my code:

public static void CreateImage(ref string sFileName, string sText, string sFontName, int iFontSize, bool bBold,
bool bCentered, int iWidth, int iHeight, Color oForeColor, Color oBackColor)
{
Bitmap oBitmap = null;
Graphics oGraphics = null;

try
{
oBitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb);

oGraphics = Graphics.FromImage(oBitmap);
oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
oGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphics.Clear(oBackColor);

Font oFont = new Font(sFontName, iFontSize);

StringFormat drawFormat = new StringFormat();
drawFormat.LineAlignment = StringAlignment.Center;
drawFormat.Alignment = (bCentered) ? StringAlignment.Center : StringAlignment.Near;

RectangleF oRectangle = new RectangleF(0, 0, iWidth, iHeight);
oGraphics.DrawString(sText, oFont, new SolidBrush(oForeColor), oRectangle, drawFormat);

sFileName = sFileName + ".gif";
oBitmap.Save(sFileName, ImageFormat.Gif);
}
catch (Exception e)
{
throw e;
}
finally
{
if (oBitmap != null)
oBitmap.Dispose();
if (oGraphics != null)
oGraphics.Dispose();
}
}
Reply to this message...
 
    
Minh Truong
Kirk,

Looks like your image suffers from dithering because .NET saves GIFs with a
fixed palette, meaning that the 256 colors that are included w/ any GIF is a
general palette -- and not optimized for the content of the GIF. So that if
your GIF contains a color not in that general palette will be dither -- and
look grainy. Try drawing the text with a color that exist in the palette
(use a bmp editor to find the exact value) -- but then the anti-alias colors
may suffer from dithering -- but at least it'll be much less noticeable.

Another thing you may want to try is to save it as a JPG w/ the highest
quality setting -- otherwise you'll notice a different kind of distortion.

Good luck.

----- Original Message -----
From: "Kirk Marple" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Monday, January 21, 2002 12:43 AM
Subject: [ngfx-drawing] best quality text rendering?

i just wrote ths neat-o command-line image generation tool for creating
graphics for my ASP.NET app, and i'm trying to get the best quality text out
of it. (btw, i'd be happy to give anybody the C# project, if they're
interested.)

the docs inside VS.NET are pretty slim, and i just want to make sure i'm
doing the right things to get the best quality anti-aliased text out. if
anybody knows GDI+ well, can they take a look at this code and see if it
looks correct?

see http://www.1968.org/images/asset_format_txt.gif for an example image
that this code created. (using Impact 10pt, not bold, not centered, 100x20,
fore: #9A0000, back: #FFFFFF)

thanks!
Kirk

---------------------

here's my code:

public static void CreateImage(ref string sFileName, string sText, string
sFontName, int iFontSize, bool bBold,
bool bCentered, int iWidth, int iHeight, Color oForeColor, Color
oBackColor)
{
Bitmap oBitmap = null;
Graphics oGraphics = null;

try
{
oBitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb);

oGraphics = Graphics.FromImage(oBitmap);
oGraphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
oGraphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
oGraphics.Clear(oBackColor);

Font oFont = new Font(sFontName, iFontSize);

StringFormat drawFormat = new StringFormat();
drawFormat.LineAlignment = StringAlignment.Center;
drawFormat.Alignment = (bCentered) ? StringAlignment.Center :
StringAlignment.Near;

RectangleF oRectangle = new RectangleF(0, 0, iWidth, iHeight);
oGraphics.DrawString(sText, oFont, new SolidBrush(oForeColor),
oRectangle, drawFormat);

sFileName = sFileName + ".gif";
oBitmap.Save(sFileName, ImageFormat.Gif);
}
catch (Exception e)
{
throw e;
}
finally
{
if (oBitmap != null)
oBitmap.Dispose();
if (oGraphics != null)
oGraphics.Dispose();
}
}

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT

Reply to this message...
 
    
Kirk Marple (VIP)
thanks for your suggestions, Minh!

unfortunately i'm bound into the colors we've already picked for our app, so
the high-quality JPEG solution sounds best.

do you have an example of how to pick the quality setting? i see the
ImageCodecInfo and EncoderParameters parameters for Bitmap.Save, but i can't
find an example of how to set them.

Kirk

On 1/20/02 10:32 PM, "Minh Truong" <Click here to reveal e-mail address> wrote:

[Original message clipped]

----------------------------------
Kirk Marple
CTO, VP of Engineering
Streaming Media Technologies, Inc.
800 5th Ave, Suite 101-358
Seattle, WA 98104
c: 206.251.4820
e: Click here to reveal e-mail address
w: http://www.streamingmediatech.com

Sent using the Entourage X Test Drive.

Reply to this message...
 
    
Minh Truong
Kirk, sorry, I haven't gotten too deep into this yet, but a friend of mine
uses PNG with very good result. Perhaphs you can try this quick option.
Most browsers support PNG these days.

----- Original Message -----
From: "Kirk Marple" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Monday, January 21, 2002 1:35 AM
Subject: [ngfx-drawing] Re: best quality text rendering?

[Original message clipped]

Reply to this message...
 
    
G. Andrew Duthie (VIP)
Kirk,

    If you're up for passing along the source code, you should post
it to aspNGcodegiveawayswap (join at
http://www.aspfriends.com/aspfriends/aspNGcodegiveawayswap.asp, send
email to Click here to reveal e-mail address), which is a new list set
up expressly for sharing code. I'm sure lots of people would love to see
it.

Regards,

--
G. Andrew Duthie
Graymad Enterprises, Inc.
Click here to reveal e-mail address
Author, ASP.NET Step By Step
http://www.amazon.com/exec/obidos/ASIN/0735612870/

[Original message clipped]

Reply to this message...
 
    
Ollie Cornes

You might also want to look at PNG. All the recent browser versions support
it and it's 24bit lossless compression so in many ways you get the best of
jpeg and gif.

I don't know if anyone else has found this, but when I enabled antialiasing
on text rendering (beta 2) the text is not rendered anti-aliased at smaller
point sizes (<13pt if I remember correctly)

Ollie

----- Original Message -----
From: "Kirk Marple" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Monday, January 21, 2002 6:35 AM
Subject: [ngfx-drawing] Re: best quality text rendering?

[Original message clipped]

Reply to this message...
 
    
Kirk Marple (VIP)
great idea, Andrew.

i just posted it up there, so if anybody here is interested, you can see my
posting on the codeswap list.

thanks,
Kirk

On 1/21/02 6:31 AM, "G. Andrew Duthie" <Click here to reveal e-mail address> wrote:

[Original message clipped]

----------------------------------
Kirk Marple
CTO, VP of Engineering
Streaming Media Technologies, Inc.
800 5th Ave, Suite 101-358
Seattle, WA 98104
c: 206.251.4820
e: Click here to reveal e-mail address
w: http://www.streamingmediatech.com

Sent using the Entourage X Test Drive.

Reply to this message...
 
    
Kirk Marple (VIP)
i've been playing around with this stuff a bit more, and i realized that
GDI+ isn't writing out the GIF or PNG files with transparency.

has anybody gotten this to work, or know what the magic option is?

thx,
k.

On 1/20/02 11:01 PM, "Minh Truong" <Click here to reveal e-mail address> wrote:

[Original message clipped]

----------------------------------
Kirk Marple
CTO, VP of Engineering
Streaming Media Technologies, Inc.
800 5th Ave, Suite 101-358
Seattle, WA 98104
c: 206.251.4820
e: Click here to reveal e-mail address
w: http://www.streamingmediatech.com

Sent using the Entourage X Test Drive.

Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
This came up a while ago and the answer was it isn't supported at
present.
What version of the framework are you using?

-----Original Message-----
From: Kirk Marple [mailto:Click here to reveal e-mail address]

i've been playing around with this stuff a bit more, and i realized that
GDI+ isn't writing out the GIF or PNG files with transparency.

has anybody gotten this to work, or know what the magic option is?

thx,
k.

On 1/20/02 11:01 PM, "Minh Truong" <Click here to reveal e-mail address> wrote:

> Kirk, sorry, I haven't gotten too deep into this yet, but a friend of
mine
> uses PNG with very good result. Perhaphs you can try this quick
option.
[Original message clipped]

Reply to this message...
 
    
Kirk Marple (VIP)
oh jeez, i can't believe it's not in there. that basically makes my image
generation tool useless for my needs.

i'm running RC4 at the moment: 1.0.3617

On 1/21/02 10:11 PM, "Ryan Trudelle-Schwarz" <Click here to reveal e-mail address> wrote:

[Original message clipped]

----------------------------------
Kirk Marple
CTO, VP of Engineering
Streaming Media Technologies, Inc.
800 5th Ave, Suite 101-358
Seattle, WA 98104
c: 206.251.4820
e: Click here to reveal e-mail address
w: http://www.streamingmediatech.com

Sent using the Entourage X Test Drive.

Reply to this message...
 
    
Ryan Trudelle-Schwarz (VIP)
Check the archives, someone may have come up with a fix but AFAIK it's
not possible. V1 may have the functionality though.

-----Original Message-----
From: Kirk Marple [mailto:Click here to reveal e-mail address]

oh jeez, i can't believe it's not in there. that basically makes my
image
generation tool useless for my needs.

i'm running RC4 at the moment: 1.0.3617

On 1/21/02 10:11 PM, "Ryan Trudelle-Schwarz" <Click here to reveal e-mail address> wrote:

[Original message clipped]

Reply to this message...
 
 
System.Drawing.Bitmap
System.Drawing.Drawing2D.SmoothingMode
System.Drawing.Font
System.Drawing.Graphics
System.Drawing.Imaging.EncoderParameters
System.Drawing.Imaging.ImageCodecInfo
System.Drawing.Imaging.ImageFormat
System.Drawing.Imaging.PixelFormat
System.Drawing.RectangleF
System.Drawing.SolidBrush
System.Drawing.StringAlignment
System.Drawing.StringFormat
System.Drawing.Text.TextRenderingHint




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification