71

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.

liori
  • 40,917
  • 13
  • 78
  • 105
  • 2
    @liori, can't you re-focus the Question so as not ask for recommendations? To me, it looks like the first part ("I already have code") can easily be expanded into topic. Maybe opening a Meta discussion **first**. Have you read [Programming is hard. Let's go shopping!](http://www.codinghorror.com/blog/2008/10/programming-is-hard-lets-go-shopping.html)? – brasofilo Dec 19 '13 at 11:59
  • @b__: I don't want to, this question already has some answers that got quite a lot of votes. If anything, a new question would be desirable more. And, actually, I'm not interested in the answer anymore, the project where I wanted to do SVG is no longer active, nor I have access to any of its code… – liori Dec 19 '13 at 13:37
  • Yep, that's the main obstacle; the edit would have to be such that doesn't invalidates the A's. OTOH, wouldn't be the first case of Shopping Question being deleted... – brasofilo Dec 19 '13 at 13:45
  • [http://www.codeproject.com/KB/cs/svgpad.aspx](http://www.codeproject.com/KB/cs/svgpad.aspx) – MusiGenesis Oct 06 '09 at 13:02

4 Answers4

42

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.

Dan Morphis
  • 1,708
  • 19
  • 23
Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
  • This seems to be the main project page: http://www.jbrowse.com/svgnet/. It looks like the library has not been developed for 6 years. I'll probably still check it. – liori Oct 06 '09 at 13:20
  • 10
    The sites pointed there are dead. As everything is BSD licensed created a GitHub repo for them: https://github.com/monoman/SvgNet – Monoman Nov 16 '10 at 12:13
  • This is pretty old and shouldn't be considered anymore. See my answer below for a library that is in active development. – thalm Aug 12 '19 at 14:42
37

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/svg-net/SVG

Edit: Just to let you know, as of January 2021:

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

thalm
  • 2,738
  • 2
  • 35
  • 49
8

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...

jdehaan
  • 19,700
  • 6
  • 57
  • 97
4

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.

Audrius
  • 2,836
  • 1
  • 26
  • 35
  • 4
    The problem is time: we'd like to spend as little time as possible on this task, and SVG support is not really vital for us. Therefore we're looking for something that could possibly be used as in-place replacement for GDI API... or at least something that would not need much adaptation. Your suggestion was also already considered by us. – liori Oct 06 '09 at 13:34
  • I see. Then SDX2000 suggestion is the most logical approach to this (-. – Audrius Oct 06 '09 at 13:50
  • 9
    Ok I'll just put in for a one year sabbatical so I can implement that. – Martin Murphy Jan 05 '10 at 04:02