Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting SVG path data into GDI+ GraphicsPath data

Is there an easy way to convert an SVG path tag into a C# System.Drawing.Drawing2D.GraphicsPath? They are both closely related and I was hoping there would be an easy to convert the SVG path data into GraphicsPath Points.

like image 683
Icemanind Avatar asked Apr 19 '11 04:04

Icemanind


3 Answers

This SVG project provides a solution in the following way:

var pathData = ...;

var graphicsPath = new GraphicsPath();

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

graphics.DrawPath(Pens.Black, graphicsPath);

It's available as a NuGet package via:

PM> Install-Package Svg
like image 118
Drew Noakes Avatar answered Oct 22 '22 20:10

Drew Noakes


There's no easy way, although SVG paths and GraphicsPath look similar and serve the same purpose, there are some differences in how things are specified and handled. One example: SVG arc definition is different from how GraphicsPath defines arcs, so you'll need to do a little bit of trigonometry to convert it.

Also check out Drawing SVG in .NET/C#?

like image 32
Igor Brejc Avatar answered Oct 22 '22 21:10

Igor Brejc


I hope this ain't late! Check out the source code of the svg viewer program from AGG: http://www.antigrain.com/svg/index.html

The source code is in C++ and uses the AGG graphics engine, but it's easy to translate to GDI+. It also handles the conversion of SVG Arc to a Bezier Arc which can then be used with GDI+.

Good luck

like image 2
user732592 Avatar answered Oct 22 '22 20:10

user732592