?

Log in

ms_dot_net

Graphics.DrawString

« previous entry |
Apr. 14th, 2010 | 12:29 pm
location: Work
mood: confusedconfused
posted by: owenblacker in ms_dot_net

I'm getting very frustrated with trying to generate text on images in .Net. Whilst the process to do so is pretty simple, the results one gets simply aren't that satisfactory.

I have the following code (really very simple, as you can see) in an IHttpHandler, which I call in a loop to iterate through some of the fonts on my system as a proof-of-concept:

/// <summary> Enables processing of HTTP Web requests. </summary> /// <param name="context"> /// An <see cref="T:System.Web.HttpContext"/> object that provides references to the /// intrinsic server objects (for example, Request, Response, Session, and Server) /// used to service HTTP requests. /// </param> /// <remarks> /// Looks for a <see cref="HttpRequest.QueryString"/> item named <c>font</c> and /// renders a 100×30px image of that font name, in that font. /// </remarks> /// <exception cref="ArgumentNullException"> /// No querystring element <c>font</c> was found. /// </exception>
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/png"; string fontName = context.Request.QueryString["font"]; Image img = new Bitmap(Width, Height); using (Graphics graphic = Graphics.FromImage(img)) { graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; graphic.DrawString( fontName, new Font(fontName, FontSize), new SolidBrush(Color.Black), new PointF(0, 0) ); } img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); }

So far so good. It does almost exactly what I'd expect it to. But the images it generates are, frankly, shit. The following image is a screengrab of me running this locally (in Google Chrome, not that that matters), with the images on the left and the font name on the right (using CSS to cause it to be in the same size and font as the image):

Screengrab of results

So why is GDI+ generating the text bolder (darker) than it should be. I understand that it won't necessarily be as beautiful as the ClearType-rendered text, but the weight of the font is just damn wrong!

So how do I get GDI+ to play ball? Anyone?

If you don't want to leave thoughts as a comment on here, feel free to email me (owen dot blacker at iris hyphen digital dot com). Cross-posted to owenblacker and ms_dot_net.

Link | Leave a comment | Share

Comments {1}

owenblacker

(no subject)

from: owenblacker
date: Apr. 14th, 2010 01:02 pm (UTC)
Link

graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; seems to help quite a bit.

I've tried adding varying the Graphics.SmoothingMode, the Graphics.PixelOffsetMode and adding StringFormat.GenericTypographic to the (overloaded) Graphics.DrawString call, none of which gives me as good a result as TextRenderingHint.AntiAliasGridFit.

Curiously, the docs suggest that TextRenderingHint.ClearTypeGridFit should be better quality again. It isn't.

Screenshot of the results of the TextRenderingHint.

Reply | Thread