Outline text with System.Drawing?
Asked Answered
I

1

22

I have the following code. Is there an easy way to put an outline on the text I am writing?

 var imageEncoder = Encoder.Quality;
 var imageEncoderParameters = new EncoderParameters(1);
 imageEncoderParameters.Param[0] = new EncoderParameter(imageEncoder, 100L);

 var productImage = GetImageFromByteArray(myViewModel.ProductImage.DatabaseFile.FileContents);
 var graphics = Graphics.FromImage(productImage);

 var font = new Font("Segoe Script", 24);
 var brush = Brushes.Orange;

 var container = new Rectangle(myViewModel.ContainerX, myViewModel.ContainerY,                                      myViewModel.ContainerWidth,                                              myViewModel.ContainerHeight);

 var stringFormat = new StringFormat {Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center};

 graphics.DrawString(customizationText, font, brush, container, stringFormat);
Insomniac answered 17/11, 2010 at 2:0 Comment(1)
I find your lack of "using" disturbing.Gladdie
Z
47

Yes. Instead of DrawString, use the following sequence of calls:

If you need to use GraphicsPath.AddString alongside Graphics.DrawString, you need to convert the font sizes, because Graphics.DrawString expects “point size” while GraphicsPath.AddString expects “em size”. The conversion formula is simply emSize = g.DpiY * pointSize / 72.

Here's a code example:

// assuming g is the Graphics object on which you want to draw the text
GraphicsPath p = new GraphicsPath(); 
p.AddString(
    "My Text String",             // text to draw
    FontFamily.GenericSansSerif,  // or any other font family
    (int) FontStyle.Regular,      // font style (bold, italic, etc.)
    g.DpiY * fontSize / 72,       // em size
    new Point(0, 0),              // location where to draw text
    new StringFormat());          // set options here (e.g. center alignment)
g.DrawPath(Pens.Black, p);
// + g.FillPath if you want it filled as well
Zaremski answered 17/11, 2010 at 2:10 Comment(4)
Thanks so much for the response, but I can't seem to get this to work. I am not sure which overload(s) to use for these methods. Would the only line I take out of my current program be the graphics.DrawString() line?Insomniac
Thanks for this info, I did just what you said... But of course I realized that there's no anti-aliasing so text looks pretty awful. :( Not sure what to do about that yet.Liselisetta
@QuinxyvonBesiex: I always set g.InterpolationMode = InterpolationMode.High;, g.SmoothingMode = SmoothingMode.HighQuality;, g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; and g.CompositingQuality = CompositingQuality.HighQuality;. One of these controls the anti-aliasing for lines and shapes.Zaremski
@Timwi, thanks! I ended up doing something along the lines of what you said, but the super-critical part I was not understanding was that I was creating the text on a graphics object that already had the image over which the text was supposed to go, so I think that was most of my problem. I then modified my code to use a separate Image/Graphics context and then copy the text back onto the image once it was rendered. Now everything works well! Thanks again.Liselisetta

© 2022 - 2024 — McMap. All rights reserved.