Background of text is mottled

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();
}
 
R

Rutger Smit

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
 
L

Lloyd Dupont

what about
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; ?
or
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit; ?
or
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; ?
 
T

Tom Dacon

See if you have ClearType turned on; it sometimes causes some unusual
aliasing artifacts.

Display Properties/Appearance/Effects...

Tom Dacon
Dacon Software Consulting
 
S

Steven Cheng[MSFT]

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
 
M

MarkMurphy

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top