Outline text with System.Drawing?

后端 未结 1 1496
礼貌的吻别
礼貌的吻别 2020-12-16 14:34

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         


        
相关标签:
1条回答
  • 2020-12-16 14:54

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

    • new GraphicsPath (creates an empty GraphicsPath)
    • GraphicsPath.AddString (the GraphicsPath object now represents the outline of the text)
    • Graphics.DrawPath (draws the outline in any Pen you want)

    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
    
    0 讨论(0)
提交回复
热议问题