将 Markdown 转换为图像 – C#
如果您需要预览 Markdown 文件,可以将其转换为图像格式。使用 Aspose.HTML for .NET 库,只需几行代码,您就可以轻松地将 Markdown 转换为 JPG、PNG、BMP、GIF 或 TIFF 文件!
本文介绍了如何使用 Aspose.HTML for .NET API 将 Markdown 转换为图像格式。您将了解到支持的转换方案,并考虑使用 C# 示例来说明这些方案。此外,您还可以试用在线 Markdown 转换器来测试 Aspose.HTML API 的功能并即时转换 Markdown。
在线 Markdown 转换器
您可以使用 Aspose.HTML for .NET API 实时将 Markdown 转换为其他格式。请从本地文件系统加载 Markdown,选择输出格式并运行示例。 保存选项是默认设置的。您将立即以单独文件的形式收到转换结果。
如果您想通过编程将 Markdown 转换为图像格式,请参阅以下 C# 代码示例。
用 C# 将 Markdown 转换为 JPG
从 Markdown 到其他格式的转换需要经过 Markdown 到 HTML 的转换阶段。例如,如果您需要将 Markdown 文档转换为 JPG 图像文件格式,下面的示例演示了如何简单地进行转换:
- 准备一个源 Markdown 文档。在示例中,我们根据代码创建一个 Markdown 文件。
- 为转换后的文件保存准备路径。
- 将 Markdown 转换为 HTML。使用
ConvertMarkdown(
sourcePath
) 方法将 Markdown 保存为 HTML 文档。 - 使用 ConvertHTML() 方法将中介 HTML 文档渲染为 JPG 图像。您需要向 ConvertHTML() 方法传递 HTMLDocument、ImageSaveOptions 和输出文件路径,以便将 HTML 转换为 JPG。
如果您想直接在代码中根据用户字符串创建 Markdown 文档并将其转换为 JPG 文件,下面的示例可以帮到您:
1// Convert Markdown to JPG using C#
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(OutputDir, "document.md");
5
6// Prepare a simple Markdown example
7string code = "### Hello, World!" +
8 "\r\n" +
9 "[visit applications](https://products.aspose.app/html/family)";
10// Create a Markdown file
11File.WriteAllText(sourcePath, code);
12
13// Prepare a path to save the converted file
14string savePath = Path.Combine(OutputDir, "document-output.jpg");
15
16// Convert Markdown to HTML
17using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
18
19// Convert HTML document to JPG image file format
20Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), savePath);
使用 ImageSaveOptions 将 Markdown 转换为 JPG
如果您要从本地文件系统转换现有的 Markdown 文档,下面的示例可以帮到您。您需要遵循以下几个步骤:
- 打开现有的 Markdown 文档。在示例中,我们从本地文件系统加载了一个 Markdown 文件( nature.md)。
- 为转换后的文件保存准备路径。
- 将 Markdown 转换为 HTML。使用转换器类的
ConvertMarkdown(
sourcePath
) 方法将 Markdown 保存为 HTML 文档。 - 创建一个新的 ImageSaveOptions 对象并指定所需的属性。
- 使用 ConvertHTML() 方法将中间 HTML 文档渲染为 JPG 图像。您需要向 ConvertHTML() 方法传递 HTMLDocument、ImageSaveOptions 和输出文件路径。
以下代码片段演示了如何使用自定义保存选项将 Markdown 转换为 JPG:
1// Convert Markdown to JPG in C# with custom settings
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(DataDir, "nature.md");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "nature-options.jpg");
8
9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
14{
15 UseAntialiasing = true,
16 HorizontalResolution = 200,
17 VerticalResolution = 200,
18 BackgroundColor = System.Drawing.Color.AliceBlue
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 950), new Margin(30, 20, 10, 10));
21
22// Convert HTML to JPG
23Converter.ConvertHTML(document, options, savePath);
ImageSaveOptions 类提供了大量属性,可让您全面控制各种参数,并改进将 Markdown 转换为 JPG 格式的过程。要了解有关 ImageSaveOptions 的更多信息,请阅读 微调转换器 一文。
在上述示例中,我们使用
UseAntialiasing
属性,用于设置此图像的渲染质量。本示例使用UseAntialiasing = true
设置渲染质量。HorizontalResolution
和VerticalResolution
属性以每英寸像素为单位设置输出图像的水平/垂直分辨率。默认情况下,这些属性为 300 dpi。BackgroundColor
属性,用于设置填充背景的颜色。默认的 BackgroundColor 是透明色。PageSetup
属性指定 page size 和 margins。
如果您想提高应用程序中渲染的图形、文本和图像的视觉质量,尤其是对清晰度和平滑边缘要求较高时,请使用 UseAntialiasing = true
。启用抗锯齿功能可通过混合边缘周围像素的颜色来平滑锯齿状边缘,从而获得更柔和、更精致的外观。
虽然 UseAntialiasing = true
可以提供更好的视觉质量,但也会增加处理时间。对于优先考虑渲染速度的应用程序,设置 UseAntialiasing = false
可能是最佳选择。
用 C# 将 Markdown 转换为 PNG
以下代码片段演示了如何将 Markdown 转换为 PNG:
1// Convert Markdown to PNG using C#
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(DataDir, "document.md");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "output.png");
8
9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Convert HTML document to PNG image file format
13Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);
Aspose.HTML 提供免费的在线 MD 到 PNG 转换器,可将 Markdown 转换为高质量的 PNG 图像,简单快捷。只需上传、转换文件,几秒钟内就能得到结果!
用 C# 将 Markdown 转换为 BMP
以下代码片段演示了如何将 Markdown 转换为 BMP:
1// Convert Markdown to BMP using C#
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(DataDir, "document.md");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "output.bmp");
8
9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Convert HTML document to BMP image file format
13Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Bmp), savePath);
用 C# 将 Markdown 转换为 GIF
下面的代码片段演示了如何将 Markdown 转换为 GIF:
1// Convert Markdown to GIF using C#
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(DataDir, "document.md");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "output.gif");
8
9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Convert HTML document to GIF image file format
13Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Gif), savePath);
用 C# 将 Markdown 转换为 TIFF
以下代码片段演示了如何将 Markdown 转换为 TIFF:
1// Convert Markdown to TIFF using C#
2
3// Prepare a path to a source Markdown file
4string sourcePath = Path.Combine(DataDir, "document.md");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "output.tiff");
8
9// Convert Markdown to HTML
10using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
11
12// Convert HTML document to TIFF image file format
13Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Tiff), savePath);
下载 Aspose.HTML for .NET库,它能让您成功、快速、轻松地将 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。
您可以使用我们的在线 MD 到 JPG 转换器 检查 Markdown 转换为 JPG 的质量。上传、转换文件并在几秒钟内获得结果。现在就免费试用我们强大的 Markdown 转 JPG 转换器吧!