在 .NET/C# 中绘制 SVG?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing SVG in .NET/C#?
提问by liori
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.Metafileand 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.Graphicsinterface.
我想使用 C# 生成一个 SVG 文件。我已经有了以 PNG 和 EMF 格式绘制它们的代码(使用框架的标准类System.Drawing.Imaging.Metafile和ZedGraph)。您建议如何使该代码适应 SVG?最好我想找到一些可以模仿System.Drawing.Graphics界面的库(免费或不免费)。
回答by Autodidact
Check out the SVG framework in C# and an SVG-GDI+ bridgeproject.
查看C# 中的SVG 框架和一个 SVG-GDI+ 桥接项目。
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.
SvgGdi 桥接器是一组使用 SvgNet 在 SVG 和 GDI+ 之间进行转换的类。这意味着任何使用 GDI+ 绘制图形的代码也可以轻松地输出 SVG,只需插入 SvgGraphics 对象即可。此对象与常规 .NET Graphics 对象完全相同,但创建了一个 SVG 树。甚至像阴影填充和线锚这样的东西也被实现了。
回答by Audrius
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.
由于 SVG 基本上是一个 XML 文档 - 您可以自己实现“绘图”。检查W3C SVG 规范中的规范。我曾经做过一次生成 SVG 签名图像,只需要几个小时和一个 Firefox 来测试生成的图像。
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.
PS,您可以创建自己的包装器来模仿 System.Drawing.Graphics,例如 DrawLine() 以附加到内部缓冲区等。
回答by jdehaan
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.
我使用了这个http://svg.codeplex.com/并且我对它非常满意。仍然存在一些错误,因此您应该查看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...
该库足以满足大多数通常的需求。对于真正花哨的东西,它需要改进思想......
回答by thalm
We have made a public fork of the C# .NET SVG library on github.
我们已经在 github 上公开了 C# .NET SVG 库。
It is much improved over the one you find on codeplex, please have a look and fork it as you like:
它比您在 codeplex 上找到的有很大改进,请随意查看并分叉它:
Edit:Just to let you know, as of June 2019:
编辑:只是为了让您知道,截至 2019 年 6 月:
While others seem dead since years, this is still active. But we could definitely use some help from other developers.
虽然其他人多年来似乎已经死了,但这仍然很活跃。但是我们绝对可以使用其他开发人员的帮助。