Question

I am captioning an image, but the corners are too sharp for letters such as M and W.

enter image description here

Is there a method for rounded corners? Here is my current method. It has long references since I'm using this in WPF and I do not want it to conflict.

public static System.Drawing.Image captionImage(System.Drawing.Image img, string text, string font, float fontSize, int left, int top)
{
    System.Drawing.FontFamily c;

    c = System.Drawing.FontFamily.Families.Where(x => x.Name.ToLower() == font.ToLower()).First();

    if (c != null)
    {
        using (System.Drawing.StringFormat sf = new System.Drawing.StringFormat())
        {
            sf.Alignment = System.Drawing.StringAlignment.Near;
            sf.LineAlignment = System.Drawing.StringAlignment.Near;

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img))
            {
                using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath())
                {
                    path.AddString(text, c, 0, fontSize, new System.Drawing.Point(left, top), sf);

                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.DrawPath(new System.Drawing.Pen(System.Drawing.Color.Black, fontSize * 0.3f), path);
                    g.FillPath(System.Drawing.Brushes.White, path);
                }
            }
        }
    }

    return img;
}
Was it helpful?

Solution

I figured it out after all.

I had to create a pen object and set its line to round.

using (System.Drawing.Pen p = new System.Drawing.Pen(System.Drawing.Color.Black, fontSize * 0.3f))
{
    p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
    g.DrawPath(p, path);
    g.FillPath(System.Drawing.Brushes.White, path);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top