本指南详细介绍了如何在 C# 中将 **Markdown 转换为 Image。它包含分步算法和运行代码示例,用于在 C# 中将 Markdown 导出为 PNG。此外,您可以修改此代码以创建不同图像格式的输出图像,包括 BMP、GIF、JPG 等。
在 C# 中将 Markdown 渲染为图像的步骤
- 通过安装 Aspose.HTML 库来准备 IDE,以便导出 markdown 文件
- 创建示例 markdown 文件
- 使用 convert_markdown 方法将加载的 markdown 内容转换为 HTML 文件格式
- 通过调用 convert_html 方法将转换后的 HTML 格式渲染为图像
上述步骤概述了在 C# 中将 *MD 转换为 PNG 的过程。作为先决条件,您必须准备好所需的安装环境。随后,通过创建示例输入文件并继续渲染为 HTML,然后渲染图像来启动该过程。
使用 C# 将 Markdown 转换为 PNG 的代码
using System.IO; | |
using Aspose.Html; | |
using Aspose.Html.Saving; | |
using Aspose.Html.Rendering.Image; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
string sourcePath = "document.md"; | |
string savePath = "document-output.png"; | |
// Prepare a simple Markdown example | |
var code = "### Hello, World!" + "\r\n" + "Convert Markdown to Image!"; | |
// Create a Markdown file | |
File.WriteAllText(sourcePath, code); | |
// Initialize ImageSaveOptions | |
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png); | |
// Convert Markdown to HTML | |
var document = Aspose.Html.Converters.Converter.ConvertMarkdown(sourcePath); | |
// Convert HTML to JPG | |
Aspose.Html.Converters.Converter.ConvertHTML(document, options, savePath); | |
System.Console.WriteLine("MD converted to image successfully"); | |
} | |
} |
此示例代码演示了如何在 C# 中将 Markdown 转换为图像。但是,您可以通过从 ImageFormat 枚举中选择任意值来修改输出图像格式,从而对其进行自定义。同样,您可以设置 ImageSaveOptions 类的许多其他属性,以根据您的要求设置图像尺寸、分辨率等。
在本教程中,您已了解了如何在 C# 中将 Markdown 导出为 JPG。另一方面,如果您想将 Markdown 转换为 Word,请阅读 在 C# 中将 Markdown 转换为 Word 上的文章。