Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

c# - Converting text graphics to SVG results in corrupted rendering

I need to convert GraphicsPath to SVG. I used the following piece of code:

SvgDocument d = new SvgDocument();
SvgPath p = new SvgPath();

var a = DrawText(tBText.Text);
string b = "M";
foreach (var item in a.PathPoints)
{
    b += " " + item.ToSvgString();
}
    
p.PathData = SvgPathBuilder.Parse(b);
d.Children.Add(p);
pictureBox1.Image = d.Draw();
d.Write("ss.svg");

DrawText function is here:

private GraphicsPath DrawText(string text)
        {
            Graphics g = pictureBox1.CreateGraphics();
            Font font = new Font("Jameel Noori Nastaleeq", 24, FontStyle.Regular);
            GraphicsPath gp = new GraphicsPath();
            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                gp.FillMode = FillMode.Winding;
                
                gp.AddString(text, font.FontFamily, (int)font.Style, font.Size, pictureBox1.ClientRectangle, sf);
                gp.ClearMarkers();
                g.Clear(Color.Transparent);
                g.DrawPath(Pens.Red, gp);

            }
            return gp;
        }

The issue is that it is not converting to SVG properly and the rendering looks corrupted. For example, the word "Hello" looks like this:

enter image description here

Where am I going wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't add a comment, but in the TaW comment, it's missing t == 161

else if (t == 1 || t == 129 || t == 161)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...