14

SVG パス タグを C# System.Drawing.Drawing2D.GraphicsPath に変換する簡単な方法はありますか? どちらも密接に関連しており、SVG パス データを GraphicsPath ポイントに簡単に変換できることを期待していました。

4

4 に答える 4

4

簡単な方法はありません。SVG のパスとパスGraphicsPathは似ていて同じ目的を果たしますが、指定方法と処理方法にはいくつかの違いがあります。1 つの例: SVG 円弧の定義は円弧の定義方法とは異なるGraphicsPathため、変換するには少し三角法を行う必要があります。

Drawing SVG in .NET/C#もチェックしてください。

于 2011-04-19T06:47:02.370 に答える
2

これが遅くないことを願っています!AGG の svg ビューアー プログラムのソース コードを確認してください: http://www.antigrain.com/svg/index.html

ソース コードは C++ で書かれており、AGG グラフィック エンジンを使用していますが、GDI+ に簡単に変換できます。また、SVG Arc から GDI+ で使用できる Bezier Arc への変換も処理します。

幸運を

于 2012-03-06T01:45:58.353 に答える
0

それほど複雑ではありません。

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;
}
于 2018-09-17T15:50:26.387 に答える