Background of text is mottled

  • Thread starter Thread starter MarkMurphy
  • Start date Start date
M

MarkMurphy

Hi,

I'm using the code below to create a background and then write some
text on it. The area behind the text appears pixelated in the output
image. I've tried output to jpeg and gif but result is the same.

private void DrawImage()
{
string s = "A TEST CAPTION";
Rectangle frame;
Font f = new Font("Arial", 10);
SolidBrush sb;
Response.ContentType = "image/jpeg";

Bitmap bmp = new Bitmap(200, 24);
Graphics g = Graphics.FromImage(bmp);

//Draw caption area
sb = new SolidBrush(Color.Gray);
frame = new Rectangle(0, 0, 200, 24);
g.FillRectangle(sb, frame);

f = new Font("Arial", 10);
sb = new SolidBrush(Color.Black);
g.DrawString(s, f, sb, 8, 4);

// Send image
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

bmp.Dispose();
g.Dispose();
sb.Dispose();
}
 
MarkMurphy said:
Hi,

I'm using the code below to create a background and then write some
text on it. The area behind the text appears pixelated in the output
image. I've tried output to jpeg and gif but result is the same.

private void DrawImage()
{
string s = "A TEST CAPTION";
Rectangle frame;
Font f = new Font("Arial", 10);
SolidBrush sb;
Response.ContentType = "image/jpeg";

Bitmap bmp = new Bitmap(200, 24);
Graphics g = Graphics.FromImage(bmp);

//Draw caption area
sb = new SolidBrush(Color.Gray);
frame = new Rectangle(0, 0, 200, 24);
g.FillRectangle(sb, frame);

f = new Font("Arial", 10);
sb = new SolidBrush(Color.Black);
g.DrawString(s, f, sb, 8, 4);

// Send image
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

bmp.Dispose();
g.Dispose();
sb.Dispose();
}


Two questions:
1: don't you have to call g.Save(); ?
2: does it get better if you add this line: g.SmoothingMode =
SmoothingMode.HighQuality; ?

Regards,
//Rutger
 
what about
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; ?
or
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; ?
or
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; ?
 
See if you have ClearType turned on; it sometimes causes some unusual
aliasing artifacts.

Display Properties/Appearance/Effects...

Tom Dacon
Dacon Software Consulting
 
Hi Mark,

As for this problem, infact it is some what caused by the image be
converted and color reduced by the web safe plate of the browser. This is
any very good means , but in addition to other members' suggestion , you
may also have a look at the following tech article:

#Optimizing Color Quantization for ASP.NET Images
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/colorquant.asp

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks all for the suggestions. It turned out in my case that I needed
to use a web-safe color for the background and use one of the text
rendering hint values. Two problems, two solutions. Much
appreciated!

Mark
 
Back
Top