SVG 转换器 – C# 代码和在线转换器

使用 C# 转换 SVG

SVG 文件是可缩放矢量图形文件,它使用基于 XML 的文本格式来描述图像的外观。可缩放一词指的是 SVG 可缩放至不同尺寸而不损失任何质量。基于文本的描述使这类文件不受分辨率的影响。它是用于网站建设和印刷图形以实现可扩展性的最常用格式之一。

Aspose.HTML 的主要亮点是转换功能。由于各种原因,需要在不同格式之间进行转换:以熟悉、方便的格式工作,或利用不同格式完成特定任务。Aspose.Html.Converters](1) 命名空间实现了对转换方法的轻松访问。它提供了多种 SVG 转换为流行格式的方法,如 PDFXPSDOCXJPGPNGBMPTIFFGIF

本节的文章介绍了支持的 SVG 转换列表以及如何使用 ConvertSVG() 方法执行转换。SVG 转换的情况可能各不相同,但只需几个必要步骤即可完成:

  1. 加载 SVG 文档。您可以从文件、SVG 代码、流或 URL 加载 SVG。
  2. 创建一个保存选项对象。您可以自定义渲染过程,以获得所需的效果。
  3. 调用 ConvertSVG() 方法之一,并向其传递所需的参数。

请看下面的 C# 代码片段,它显示了使用指定的 ImageSaveOptions 将 SVG 转换为 PNG 的过程:

 1// Convert SVG to PNG in C# with custom settings
 2
 3// Prepare a path to a source SVG file
 4string documentPath = Path.Combine(DataDir, "flower1.svg");
 5
 6// Prepare a path to save the converted file
 7string savePath = Path.Combine(OutputDir, "flower-options.png");
 8
 9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue 
13ImageSaveOptions options = new ImageSaveOptions()
14{
15    HorizontalResolution = 200,
16    VerticalResolution = 200,
17    BackgroundColor = System.Drawing.Color.AliceBlue,
18    UseAntialiasing = true,
19};
20
21// Convert SVG to PNG
22Converter.ConvertSVG(document, options, savePath);

在示例中, SVGDocument(address) 构造函数从文件中初始化一个 SVG 文档。ImageSaveOptions() 构造函数创建了一个新的 ImageSaveOptions 对象,该对象具有 PNG ImageFormat、BackgroundColor 和其他属性。然后, ConvertSVG() 方法将 SVGDocument、ImageSaveOptions 和输出路径转换为 PNG。

在线 SVG 转换器

您可以使用 Aspose.HTML API 将 SVG 实时转换为其他格式。首先,从本地驱动器加载 SVG 文件,然后运行示例。示例中的保存选项为默认设置。您将立即以单独文件的形式收到转换结果。

                
            

Aspose.HTML 提供免费的在线 SVG 转换器 用于将 SVG 文件转换为各种流行格式。您可以轻松地将 SVG 转换为 PDF、SVG 转换为 XPS、 SVG 转换为 JPGSVG 转换为 PNG、SVG 转换为 BMP、SVG 转换为 TIFF 或 SVG 转换为 GIF。只需选择文件,选择要转换的格式,就大功告成了。速度快,完全免费!

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.