Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将SVG路径数据转换为GDI+;图形数据_C#_Svg_Gdi+_Graphicspath - Fatal编程技术网

C# 将SVG路径数据转换为GDI+;图形数据

C# 将SVG路径数据转换为GDI+;图形数据,c#,svg,gdi+,graphicspath,C#,Svg,Gdi+,Graphicspath,有没有一种简单的方法可以将SVG路径标记转换为C#System.Drawing.Drawing2D.GraphicsPath?它们都是密切相关的,我希望有一个简单的方法可以将SVG路径数据转换为GraphicsPath点。没有简单的方法,尽管SVG路径和GraphicsPath看起来相似,用途相同,但在如何指定和处理方面存在一些差异。一个例子:SVG圆弧定义与GraphicsPath定义圆弧的方式不同,因此您需要做一些三角运算来转换它 也请查看我希望这不会太晚!查看AGG中svg查看器程序的源代

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

没有简单的方法,尽管SVG路径和
GraphicsPath
看起来相似,用途相同,但在如何指定和处理方面存在一些差异。一个例子:SVG圆弧定义与
GraphicsPath
定义圆弧的方式不同,因此您需要做一些三角运算来转换它


也请查看

我希望这不会太晚!查看AGG中svg查看器程序的源代码:

源代码是C++,使用AGG图形引擎,但很容易翻译成GDI+。它还处理SVG圆弧到贝塞尔圆弧的转换,然后可以与GDI+一起使用

好运

通过以下方式提供解决方案:

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

没那么复杂

如果svg路径仅包含
mlqzzm
函数,则您的方法如下所示:

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;
}
private GraphicsPath svgMLQZToGraphicsPath(字符串svgString)
{
GraphicsPath GraphicsPath=新的GraphicsPath();
浮动[]x=新浮动[4];
浮动[]y=新浮动[4];
字符串prev=“”;
string[]splits=svgString.Split(“”);
对于(int s=0;s=2&&splits[s]。子字符串(0,2)==“ZM”)
{
x[0]=float.Parse(拆分[s].子字符串(2).Replace('.',',');
y[0]=float.Parse(拆分[s+1]。替换(“,”,“);
s++;
graphicsPath.StartFigure();
prev=“M”;
}
}
其他的
{
字符串ok=@“^[a-zA-Z]*$”;
如果(!Regex.IsMatch(拆分[s+1].子字符串(0,1),确定))
{
字符串替换=prev+splits[s+1];
拆分[s+1]=替换;
}
}
}
返回图形SPATH;
}

您是如何访问SvgPathBuilder的?这是一个内部类。。反射?@Pogrindis,你在用NuGet软件包吗?也许我用的是旧版本。对不起,已经有一段时间了,我不记得了。不过我不会使用反射——只使用显示的代码。@Pogrindis,看起来这个类是公共的:谢谢!看起来我使用的是旧版本!:(指定的链接不再存在。