SVG可以算是目前较为火热的图像文件格式了,严格来说应该是一种开放标准的矢量图形语言,可设计激动人心的、高分辨率的Web图形页面。
你是否在寻找一款工具能够处理SVG格式文件么,好消息来啦!用于.NET平台SVG文件处理的API——Aspose.SVG for .NET在平台重磅上线啦!
使用Aspose.SVG可以轻松加载,保存和转换SVG文件,以及通过其文档对象模型(DOM)读取和遍历文件的元素。API独立于任何其他软件,使开发人员无需使用该格式的基本细节,即可使用SVG文件。
使用C#创建新的SVG文件
API提供了SVGDocument 类来创建SVG文件,可以进一步使用该对象来用SVG元素填充文档。完成后,还可以使用SVGDocument.Save()方法保存文件。以下代码示例显示了如何从头开始创建SVG文件。
下面的代码示例演示如何在C#中创建空的SVG文件。
using (var document = new SVGDocument()){ // do some actions over the document here...}
API提供了SVGDocument 类来创建SVG文件,可以进一步使用该对象来用SVG元素填充文档。完成后,还可以使用SVGDocument.Save()方法保存文件。以下代码示例显示了如何从头开始创建SVG文件。
下面的代码示例演示如何在C#中使用用户定义的字符串创建SVG。
using (var document = new SVGDocument("", ".")){ // do some actions over the document here...}
string dataDir = RunExamples.GetDataDir_Data();using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg"))){ const string @namespace = "http://www.w3.org/2000/svg"; var circle = (SVGCircleElement)document.CreateElementNS(@namespace, "circle"); circle.Cx.BaseVal.Value = 50; circle.Cy.BaseVal.Value = 50; circle.R.BaseVal.Value = 40; var g = document.QuerySelector("g"); g.AppendChild(circle); Console.WriteLine(g.OuterHTML);}
在C#中读取SVG文件
如果想读取现有的SVG文件,则可以使用SVGDocument类加载它。只需创建一个新对象并使用SVG文件的路径对其进行初始化,如下面的代码示例所示。
string dataDir = RunExamples.GetDataDir_Data();using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg"))){ // do some actions over the document here...}
将SVG文件转换为其他格式
API还可以将SVG文件转换为其他各种文件格式。支持的转换包括:
- SVG转PDF
- SVG到XPS
- SVG到图像(JPEG,PNG,BMP,GIF,TIFF)
以下代码示例显示了如何在C#中将SVG文件转换为PDF。
string dataDir = RunExamples.GetDataDir_Data();using (var document = new SVGDocument(Path.Combine(dataDir, "smiley.svg"))){ var options = new PdfRenderingOptions() { PageSetup = { AnyPage = new Page(new Size(500, 500)) } }; using (var device = new PdfDevice(options, dataDir + "smiley_out.pdf")) { document.RenderTo(device); }}
还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。可联系联系客服快速了解!
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!