本基本教程将指导您如何在 C#** 中**创建 SVG。这些步骤和代码片段演示了如何使用 C# 以编程方式生成 SVG 图像。我们只需要在 .NET 应用程序中使用几行代码进行几次 API 调用即可完成任务。
在 C# 中创建 SVG 的步骤
- 使用 NuGet 包管理器设置项目以安装 Aspose.SVG for .NET
- 将 SVG 内容指定为字符串
- 使用字符串创建 SVGDocument 类的实例
- 将文件另存为 SVG 图像
这些步骤描述了如何使用 C# 绘制 SVG 图像文件。您可以整体更新 SVG 字符串,或者根据特定要求更改某些参数的值以创建不同的 SVG 图像。
在 C# 中创建 SVG 的代码
namespace CreateSvgInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to create SVG in CSharp | |
{ | |
// Create and instantiate a license to create SVG file without trial version watermark | |
Aspose.Svg.License licZipFolder= new Aspose.Svg.License(); | |
licZipFolder.SetLicense("Aspose.Svg.lic"); | |
// Specify SVG content as string | |
string documentContent = "<svg xmlns=\"http://www.w3.org/2000/svg\"><circle cx=\"400\" cy=\"300\" r=\"200\" /></svg>"; | |
// Initialize SVGDocument class object using the SVG string | |
Aspose.Svg.SVGDocument document = new Aspose.Svg.SVGDocument(documentContent, "."); | |
// Save the resultant file as SVG on disk | |
document.Save("circle.svg", Aspose.Svg.Saving.SVGSaveFormat.SVG); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
上面的代码片段演示了如何在 .NET 应用程序中使用 C# 创建 SVG 图像。由于 SVG 与基于 XML 的语言一致,因此它支持嵌入内容。因此,该 API 还允许您通过在 SVG 字符串中使用"foreignObject"标签来嵌入 HTML 内容。
在本文中,我们学习了如何在 C# 中创建 SVG 图像。但是,如果您想学习将 SVG 图像转换为 PDF 文件,请参阅 如何在 C# 中将 SVG 转换为 PDF 上的文章。