用 C# 将 SVG 转换为 BMP
BMP 文件代表位图图像文件,用于存储高质量位图数字图像。使用 Aspose.HTML for .NET,您可以通过编程将 SVG 转换为 BMP 格式,并完全控制各种转换参数。在本文中,您将了解如何使用转换器类的 ConvertSVG() 方法将 SVG 转换为 BMP,以及如何应用 ImageSaveOptions 方法。此外,您还可以尝试使用在线 SVG 转换器来测试 Aspose.HTML API 的功能并即时转换 SVG。
在线 SVG 转换器
您可以使用 Aspose.HTML API 将 SVG 实时转换为其他格式。请从本地文件系统加载 SVG,选择输出格式并运行示例。保存选项为默认设置。您将立即以单独文件的形式收到转换结果。
如果您想通过编程将 SVG 转换为 BMP 图像,请参阅以下 C# 代码示例。
只需一行代码即可将 SVG 转换为 BMP
Converter 类的静态方法主要用作将 SVG 文件转换为各种格式的最简单方法。只需一行代码,您就可以在 C# 应用程序中将 SVG 转换为 BMP!
在下面的示例中,我们将本地文件系统中的 SVG 文件( shapes.svg)转换并保存到本地文件系统中。
1// Convert SVG to BMP in C#
2
3// Invoke the ConvertSVG() method for SVG to BMP conversion
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Bmp), Path.Combine(OutputDir, "convert-with-single-line.bmp"));
将 SVG 转换为 BMP
使用 ConvertSVG() 方法将文件转换为另一种格式是一系列操作,其中包括文件加载和保存。在下面的示例中,我们通过代码创建一个 SVG 文件。
- 为 SVG 文档编写代码
- 创建一个新的
ImageSaveOptions 对象,其属性为
ImageFormat.Bmp
。默认情况下,格式属性为 PNG。 - 使用转换器类的
ConvertSVG(
content
,baseUri
,options
,outputPath
) 方法将 SVG 保存为 BMP 图像。
请看下面的 C# 代码片段,它显示了使用 Aspose.HTML for .NET 将 SVG 转换为 BMP 的过程。
1// Convert SVG to BMP using C#
2
3// Prepare SVG code
4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
5 "<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='10' />" +
6 "</svg>";
7
8// Prepare a path to save the converted file
9string savePath = Path.Combine(OutputDir, "circle.bmp");
10
11// Create an instance of the ImageSaveOptions class
12ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
13
14// Convert SVG to BMP
15Converter.ConvertSVG(code, ".", options, savePath);
使用 ImageSaveOptions 将 SVG 转换为 BMP
要使用指定的 ImageSaveOptions 将 SVG 转换为 BMP,需要遵循以下几个步骤:
- 使用 SVGDocument 类的一个 SVGDocument() 构造函数加载一个 SVG 文件。( flower1.svg).
- 创建一个具有 BMP 图像格式的新 ImageSaveOptions 对象,并指定保存选项。默认情况下,格式属性为 PNG。
- 使用 ConvertSVG() 方法将 SVG 保存为 BMP 图像。您需要向 ConvertSVG() 方法传递 SVGDocument、ImageSaveOptions 和输出文件路径,以便将 SVG 转换为 BMP。
以下 C# 代码片段展示了如何使用自定义保存选项将 SVG 转换为 BMP:
1// Convert SVG to BMP 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.bmp");
8
9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Initialize ImageSaveOptions. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp)
14{
15 HorizontalResolution = 200,
16 VerticalResolution = 200,
17 BackgroundColor = System.Drawing.Color.AliceBlue,
18 UseAntialiasing = true,
19};
20
21// Convert SVG to BMP
22Converter.ConvertSVG(document, options, savePath);
ImageSaveOptions() 构造函数初始化 ImageSaveOptions 类的实例,并将其传递给 ConvertSVG() 方法。ConvertSVG() 方法接收 document
, options
, 输出文件路径 savePath
并执行转换操作。
在上述示例中,我们使用
BackgroundColor
属性,用于设置填充背景的颜色。默认的 BackgroundColor 是透明色;HorizontalResolution
和VerticalResolution
属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi;UseAntialiasing
属性,用于设置此图像的渲染质量。本示例使用UseAntialiasing = true
设置渲染质量。
如果您想提高应用程序中渲染的图形、文本和图像的视觉质量,尤其是对清晰度和平滑边缘要求较高时,请使用 UseAntialiasing = true
。启用抗锯齿功能可通过混合边缘周围像素的颜色来平滑锯齿状边缘,从而获得更柔和、更精致的外观。
虽然 UseAntialiasing = true
可以提供更好的视觉质量,但也会增加处理时间。对于优先考虑渲染速度的应用程序,设置 UseAntialiasing = false
可能是最佳选择。
图中展示了 flower-options.bmp 文件的片段。
要了解有关 ImageSaveOptions 的更多信息,请阅读 微调转换器 一文。
使用我们的在线 SVG 到 BMP 转换器 检查 SVG 到 BMP 转换的质量。上传、转换文件并在几秒钟内获得结果。现在就免费试用我们强大的 SVG 到 BMP 转换器!