将SVG路径数据转换为GDI + GraphicsPath数据

问题描述 投票:11回答:4

有没有一种简单的方法可以将SVG路径标记转换为C#System.Drawing.Drawing2D.GraphicsPath?它们都是密切相关的,我希望可以很容易地将SVG路径数据转换为GraphicsPath Points。

c# svg gdi+ graphicspath
4个回答
6
投票

This SVG project以下列方式提供解决方案:

var pathData = ...;

var graphicsPath = new GraphicsPath();

foreach (var segment in SvgPathBuilder.Parse(pathData))
    segment.AddToPath(graphicsPath);

graphics.DrawPath(Pens.Black, graphicsPath);

它可以通过以下方式作为NuGet包提供:

PM> Install-Package Svg

4
投票

没有简单的方法,虽然SVG路径和GraphicsPath看起来相似并且用于相同的目的,但在指定和处理事物方面存在一些差异。一个例子:SVG弧定义与GraphicsPath定义弧的方式不同,所以你需要做一些三角法来转换它。

还可以看看Drawing SVG in .NET/C#?


1
投票

我希望这不迟!查看AGG:http://www.antigrain.com/svg/index.html中svg viewer程序的源代码

源代码使用C ++并使用AGG图形引擎,但很容易转换为GDI +。它还处理SVG Arc到Bezier Arc的转换,然后可以与GDI +一起使用。

祝好运


-2
投票

这并不复杂。

如果svg路径只包含M L Q Z ZM函数,则您的方法如下所示:

private GraphicsPath svgMLQZToGraphicsPath(string svgString)
{
    GraphicsPath graphicsPath = new GraphicsPath();
    float[] x = new float[4];
    float[] y = new float[4];
    string prev = "";
    string[] splits = svgString.Split(' ');
    for (int s = 0; s < splits.Length; s++)
    {
        if (splits[s].Substring(0, 1) == "M")
        {
            x[0] = float.Parse(splits[s].Substring(1).Replace('.', ','));
            y[0] = float.Parse(splits[s + 1].Replace('.', ','));
            s++;
            prev = "M";
            graphicsPath.StartFigure();
        }
        else if (splits[s].Substring(0, 1) == "L")
        {
            x[1] = float.Parse(splits[s].Substring(1).Replace('.', ','));
            y[1] = float.Parse(splits[s + 1].Replace('.', ','));
            graphicsPath.AddLine(new PointF(x[0], y[0]), new PointF(x[1], y[1]));
            x[0] = x[1]; // x[1] = new float();
            y[0] = y[1]; //y[1] = new float();
            s++;
            prev = "L";
        }
        else if (splits[s].Substring(0, 1) == "Q")
        {
            x[1] = x[0] + (2 / 3) * (float.Parse(splits[s].Substring(1).Replace('.', ',')) - x[0]);
            y[1] = y[0] + (2 / 3) * (float.Parse(splits[s + 1].Replace('.', ',')) - y[0]);
            x[3] = float.Parse(splits[s + 2].Replace('.', ','));
            y[3] = float.Parse(splits[s + 3].Replace('.', ','));
            x[2] = x[3] + (2 / 3) * (float.Parse(splits[s].Substring(1).Replace('.', ',')) - y[3]);
            y[2] = y[3] + (2 / 3) * (float.Parse(splits[s + 1].Replace('.', ',')) - y[3]);
            graphicsPath.AddBezier(new PointF(x[0], y[0]), new PointF(x[1], y[1]), new PointF(x[2], y[2]), new PointF(x[3], y[3]));
            x[0] = x[3]; 
            y[0] = y[3]; 
            s = s + 3;
            prev = "Q";
        }
        else if (splits[s].Substring(0, 1) == "Z")
        {
            graphicsPath.CloseFigure();
            if (splits[s].Length >= 2 && splits[s].Substring(0, 2) == "ZM")
            {
                x[0] = float.Parse(splits[s].Substring(2).Replace('.', ','));
                y[0] = float.Parse(splits[s + 1].Replace('.', ','));
                s++;
                graphicsPath.StartFigure();
                prev = "M";
            }
        }
        else
        {
            string ok = @"^[a-zA-Z]*$";
            if (!Regex.IsMatch(splits[s + 1].Substring(0, 1), ok))
            {
                string replace = prev + splits[s + 1];
                splits[s + 1] = replace;
            }
        }
    }
    return graphicsPath;
}
© www.soinside.com 2019 - 2024. All rights reserved.