Drawing SVG in .NET/C#? [closed]

2019-01-08 11:53发布

问题:

I'd like to generate an SVG file using C#. I already have code to draw them in PNG and EMF formats (using framework's standard class System.Drawing.Imaging.Metafile and ZedGraph). What could you recommend to do to adapt that code to SVG? Preferably I'd like to find some library (free or not) that would mimic System.Drawing.Graphics interface.

回答1:

Check out the SVG framework in C# and an SVG-GDI+ bridge project.

From the above web page...

The SvgGdi bridge is a set of classes that use SvgNet to translate between SVG and GDI+. What this means is that any code that uses GDI+ to draw graphics can easily output SVG as well, simply by plugging in the SvgGraphics object. This object is exactly the same as a regular .NET Graphics object, but creates an SVG tree. Even things like hatched fills and line anchors are implemented.



回答2:

We have made a public fork of the C# .NET SVG library on github.

It is much improved over the one you find on codeplex, please have a look and fork it as you like:

https://github.com/vvvv/SVG

Edit: Just to let you know, as of December 2014:

While others seem dead since years, this is still active. But we could definitely use some help from other developers.



回答3:

http://www.codeproject.com/KB/cs/svgpad.aspx



回答4:

I used this one http://svg.codeplex.com/ and I am quite satisfied with it. Still has some bugs so you should have a look at the patches in http://svg.codeplex.com/SourceControl/PatchList.aspx.

When I discover mistakes I can solve I post them directly there. But it takes some time to be evaluated by the guys there. It's a better idea to have a look at the patches and apply them yourself.

The library is reasonably sufficient for most usual needs. for really fancy stuff, it needs to be improved thought...



回答5:

As SVG is basically a XML document - you can implement "drawing" yourself. Check the specs at W3C SVG spec. I did it once to generate SVG signature images, all it took was a couple of hours and a firefox to test the generated image.

Of course this applies if you are generating image from user input or if you do not mind spending some time doing conversion from another vector image format.

P.S. you can create your own wrapper to mimic System.Drawing.Graphics, e.g. DrawLine() to append to the internal buffer and so on.