Advertise with us

C# windows form text outline?

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
        );
    }

Last updated:12/23/2013 5:11:09 AM

1 Answers

Pravesh Singh
Pravesh Singh

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.

Answer