保存 SVG 文档 – C# – Aspose.SVG for .NET
保存 SVG 文档
您需要执行的大多数任务都需要保存文档。加载现有文件或从头开始创建 SVG 文档后,您可以使用 SVGDocument.Save() 方法之一保存更改。有一些重载方法允许将 SVG 保存到文件、流、Zip 存档或 URL。
在本文中,我们将回顾保存为相同格式的 SVG 文档。您将了解如何使用 SVGDocument 类的 Save() 方法保存 SVG 文件。此外,Aspose.SVG for .NET 提供了 ResourceHandler 类,允许将带有资源的 SVG 文档保存到流中并进行管理。
将 SVG 转换和渲染为其他格式的场景可在 如何转换 SVG 文件 部分中查看。
将 SVG 保存到文件
以下代码片段演示了如何使用 SVGDocument.Save() 方法将 SVG 文档保存到文件中:
1using Aspose.Svg;
2using System.IO; 1// Save SVG to file using C#
2
3// Prepare a path to save an SVG document
4string documentPath = Path.Combine(OutputDir, "circles_out.svg");
5
6// Load the SVG document from a file
7using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "circles.svg")))
8{
9 // Work with the document
10
11 // Save SVG to a file
12 document.Save(documentPath);
13}将 SVG 保存到 URL
需要为文档
lineto.svg 保存指定完整的 Url 路径,并将 url 对象传递给 Save() 方法,该方法将文档保存到 Url 指定的文件中。以下代码示例演示如何将文档保存到 Url:
1using Aspose.Svg;
2using System.IO; 1// Save SVG to Url using C#
2
3// Set a full path for an SVG document saving
4Url url = new Url(Path.Combine(OutputUrl, "lineto_out.svg"), Directory.GetCurrentDirectory());
5
6// Load the SVG document from a file
7using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
8{
9 // Work with the document
10
11 // Save the document to a Url
12 document.Save(url);
13}将 SVG 保存到本地文件系统存储
SVG 文档可以包含不同的资源,例如 CSS、外部图像和文件。 Aspose.SVG 提供了一种将 SVG 与所有链接文件一起保存的方法 – ResourceHandler 类是为了将 SVG 内容和资源保存到流而开发的。此类负责处理资源并提供允许您控制对每个资源执行的操作的方法。
让我们考虑一个将 SVG 资源保存到用户指定的本地文件存储的示例。源
with-resources.svg 文档及其链接图像文件位于同一目录中。
FileSystemResourceHandler(customOutDir) 构造函数采用指示保存资源文档的路径,并创建一个 FileSystemResourceHandler 对象。
Save(resourceHandler) 方法获取此对象并将 SVG 保存到输出存储。
1using System.IO;
2using Aspose.Svg.IO; 1// Save an SVG document along with its external resources in C#
2
3// Prepare a path to a source SVG file
4string inputPath = Path.Combine(DataDir, "with-resources.svg");
5
6// Prepare a full path to an output directory
7string customOutDir = Path.Combine(Directory.GetCurrentDirectory(), "./../../../../tests-out/saving/");
8
9// Load an SVG document from a file
10using (SVGDocument doc = new SVGDocument(inputPath))
11{
12 // Save SVG with resources
13 doc.Save(new FileSystemResourceHandler(customOutDir));
14}将 SVG 保存到 Zip 存档
您可以通过创建 ZipResourceHandler 类来实现
ResourceHandler。 ZipResourceHandler 类中的
HandleResource() 方法用于自定义如何处理单个资源并将其存储在 Zip 存档中的行为。它允许您创建包含 SVG 文档和相关资源的结构化压缩存档,使其适合存档和存储优化等场景。
在以下示例中,ZipResourceHandler类用于将
with-resources.svg 文档及其链接资源保存到 Zip 存档:
1using System.IO;
2using Aspose.Svg.IO;
3using System.IO.Compression; 1// Save SVG to a Zip Archive using C#
2
3// Prepare a path to a source SVG file
4string inputPath = Path.Combine(DataDir, "with-resources.svg");
5
6string dir = Directory.GetCurrentDirectory();
7
8// Prepare a full path to an output Zip storage
9string customArchivePath = Path.Combine(dir, "./../../../../tests-out/saving/archive.zip");
10
11// Load an SVG document
12using (SVGDocument doc = new SVGDocument(inputPath))
13{
14 // Initialize an instance of the ZipResourceHandler class
15 using (ZipResourceHandler resourceHandler = new ZipResourceHandler(customArchivePath))
16 {
17 // Save SVG with resources to a Zip archive
18 doc.Save(resourceHandler);
19 }
20}ResourceHandler 类旨在供客户实现。 ZipResourceHandler 类扩展了 ResourceHandler 基类,并提供了一种便捷的方法来管理在 Aspose.SVG for .NET 库的上下文中处理和存储与 SVG 文档链接的资源到 Zip 存档的整个过程:
1// Custom resource handler to save SVG with resources into a ZIP archive
2
3internal class ZipResourceHandler : ResourceHandler, IDisposable
4{
5 private FileStream zipStream;
6 private ZipArchive archive;
7 private int streamsCounter;
8 private bool initialized;
9
10 public ZipResourceHandler(string name)
11 {
12 DisposeArchive();
13 zipStream = new FileStream(name, FileMode.Create);
14 archive = new ZipArchive(zipStream, ZipArchiveMode.Update);
15 initialized = false;
16 }
17
18 public override void HandleResource(Resource resource, ResourceHandlingContext context)
19 {
20 string zipUri = (streamsCounter++ == 0
21 ? Path.GetFileName(resource.OriginalUrl.Href)
22 : Path.Combine(Path.GetFileName(Path.GetDirectoryName(resource.OriginalUrl.Href)),
23 Path.GetFileName(resource.OriginalUrl.Href)));
24 string samplePrefix = String.Empty;
25 if (initialized)
26 samplePrefix = "my_";
27 else
28 initialized = true;
29
30 using (Stream newStream = archive.CreateEntry(samplePrefix + zipUri).Open())
31 {
32 resource.WithOutputUrl(new Url("file:///" + samplePrefix + zipUri)).Save(newStream, context);
33 }
34 }
35
36 private void DisposeArchive()
37 {
38 if (archive != null)
39 {
40 archive.Dispose();
41 archive = null;
42 }
43
44 if (zipStream != null)
45 {
46 zipStream.Dispose();
47 zipStream = null;
48 }
49
50 streamsCounter = 0;
51 }
52
53 public void Dispose()
54 {
55 DisposeArchive();
56 }
57}将 SVG 保存到内存流
MemoryResourceHandler 类中的
ResourceHandler 类实现允许将 SVG 保存到内存流。以下代码演示了如何使用MemoryResourceHandler类在内存中存储 SVG 文档,收集和打印有关已处理资源的信息。
- 使用指定的 SVG 文件路径初始化 SVG 文档。
- 创建
MemoryResourceHandler类的实例。此类旨在在资源处理过程中捕获和存储内存流中的资源。 - 调用 SVG 文档的
Save()方法并将MemoryResourceHandler实例作为参数传递给它。这会将MemoryResourceHandler的资源处理逻辑与 SVG 文档保存过程相关联。 - 使用
MemoryResourceHandler的PrintInfo()方法打印有关已处理资源的信息。
1using System.IO;
2using Aspose.Svg.IO;
3using System.Collections.Generic; 1// Save an SVG document to memory with a resource handler in C#
2
3// Prepare a path to a source SVG file
4string inputPath = Path.Combine(DataDir, "with-resources.svg");
5
6// Initialize an SVG document
7using (SVGDocument doc = new SVGDocument(inputPath))
8{
9 // Create an instance of the MemoryResourceHandler class and save SVG to memory
10 MemoryResourceHandler resourceHandler = new MemoryResourceHandler();
11 doc.Save(resourceHandler);
12 resourceHandler.PrintInfo();
13}示例运行后,将打印有关内存存储的消息:
uri:memory:///with-resources.svg, length:556uri:memory:///photo.png, length:57438
ResourceHandler 是支持输出流的创建和管理的基类。 MemoryResourceHandler 类允许您捕获并将资源存储在内存流中,提供动态且灵活的方式来处理资源,而无需将资源物理保存到文件系统。以下代码片段展示了 MemoryResourceHandler 类中 ResourceHandler 的实现:
1// Handle and save SVG resources to memory streams in C#
2
3internal class MemoryResourceHandler : ResourceHandler
4{
5 public List<Tuple<Stream, Resource>> Streams;
6
7 public MemoryResourceHandler()
8 {
9 Streams = new List<Tuple<Stream, Resource>>();
10 }
11
12 public override void HandleResource(Resource resource, ResourceHandlingContext context)
13 {
14 MemoryStream outputStream = new MemoryStream();
15 Streams.Add(Tuple.Create<Stream, Resource>(outputStream, resource));
16 resource
17 .WithOutputUrl(new Url(Path.GetFileName(resource.OriginalUrl.Pathname), "memory:///"))
18 .Save(outputStream, context);
19 }
20
21 public void PrintInfo()
22 {
23 foreach (Tuple<Stream, Resource> stream in Streams)
24 Console.WriteLine($"uri:{stream.Item2.OutputUrl}, length:{stream.Item1.Length}");
25 }
26}您可以从 GitHub 下载完整的示例和数据文件。关于从 GitHub 下载并运行示例,您可以从 如何运行示例 部分找到。
您可以尝试使用我们的 免费在线 SVG 转换器 将 SVG 文档转换为各种其他格式。