我想在ASP.NET中将一些文本渲染为图像。它运行良好,但即使我通过更改图形TextRenderingHint选项打开AntiAlias(或ClearType),文本也会变得非常难看(文本周围有灰色像素)。
以下是相关代码:
float width;
float height;
System.Drawing.Text.PrivateFontCollection fontcollection = new System.Drawing.Text.PrivateFontCollection();
// Add the custom font families
fontcollection.AddFontFile(Server.MapPath("./Fonts/" + fontfile));
Bitmap image = new Bitmap(10, 10);
Graphics graphic = Graphics.FromImage(image);
Font font = new Font(fontcollection.Families.First(), fontsize, style);
SizeF size = graphic.MeasureString(text, font);
width = size.Width;
height = size.Height;
image = new Bitmap(Convert.ToInt32(width), Convert.ToInt32(height));
graphic = Graphics.FromImage(image);
graphic.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
graphic.DrawString(text, font, Brushes.Black, new PointF(0, 0));
Response.ContentType = "image/jpeg";
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
这里是生成图像的链接(缩放):
Unzoomed image:
我该如何解决这个问题?
答案 0 :(得分:2)
制作图像格式PNG。
默认的JPEG压缩无用。