C# windows form text outline?

Total Post:159

Points:1117
C# 
C#
 3901  View(s)
Ratings:
Rate this:

I'm trying to make the text outline. I use this code and got two errors in

using (GraphicsPath = GetStringPath(s, dpi, rect, font, format))
{            
    g.DrawPath(Pens.Black, path);
}

The visual studio under lines GraphicsPath and path

Here is my code's overview.

  GraphicsPath GetStringPath(string s, float dpi, RectangleF rect, Font font, StringFormat format)
    {
        GraphicsPath path = new GraphicsPath();
        // Convert font size into appropriate coordinates
        float emSize = dpi * font.SizeInPoints / 72;
        path.AddString(s, font.FontFamily, (int)font.Style, emSize, rect, format);
        return path;
    }
    private void btnHeader_Paint(object sender, PaintEventArgs e)
    {   
        Graphics g = e.Graphics;
        String s = "P9 area Terminal 1";
        dynamic btn = (Button)sender;
        dynamic drawBrush = new SolidBrush(btn.ForeColor);
        dynamic sf = new StringFormat
        {
            Alignment = StringAlignment.Center,
            LineAlignment = StringAlignment.Center
        };
        g.FillRectangle(
         new System.Drawing.Drawing2D.LinearGradientBrush(PointF.Empty, new PointF(0, btn.Height), Color.CornflowerBlue, Color.MediumBlue),
         new RectangleF(PointF.Empty, btn.Size));
        btnHeader.Text = string.Empty;
        RectangleF rect = this.ClientRectangle;
        Font font = this.Font;
        StringFormat format = StringFormat.GenericTypographic;
        float dpi = g.DpiY;
        using (GraphicsPath = GetStringPath(s, dpi, rect, font, format))
        {
            g.DrawPath(Pens.Black, path);
        }
        g.DrawString(s, btn.Font, drawBrush, e.ClipRectangle, sf);
        drawBrush.Dispose();
        sf.Dispose();
        ControlPaint.DrawBorder(
                e.Graphics, btnHeader.ClientRectangle,
                    Color.Black,3,ButtonBorderStyle.Solid,
                    Color.Black,3,ButtonBorderStyle.Solid,
                    Color.Black,3,ButtonBorderStyle.Solid,
                    Color.Black,3,ButtonBorderStyle.Solid
        );
    }

  1. Post:412

    Points:2888
    Re: C# windows form text outline?

    Hi Samuel,

    You're not storing the return value of GetStringPath appropriately.

    Try this:

    using (var path = GetStringPath(s, dpi, rect, font, format))
    {
        g.DrawPath(Pens.Black, path);
    }

    If this doesn't work, then update your original question with the exact error messages you're getting.

      Modified On Apr-04-2018 12:51:42 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!