Convert Markdown to Image – MD to JPG, PNG, BMP, GIF, TIFF

If you need to preview a Markdown file, you can convert it to image formats. Using the Aspose.HTML class library, you can easily convert Markdown into JPG, PNG, BMP, GIF or TIFF files with just a few lines of code!

This article provides information on how to convert Markdown to Image formats using the Aspose.HTML API. You will learn about the supported conversion scenarios and consider C# examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML API functionality and convert Markdown on the fly.

Online Markdown Converter

You can convert Markdown to other formats with Aspose.HTML for .NET API in real time. Please load Markdown from the local file system, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert Markdown to Image formats programmatically, please see the following C# code examples.

Convert Markdown to JPG in C#

Conversions from Markdown to other formats go through the Markdown to HTML conversion stage. If your scenario is required rendering Markdown document, for instance, to the JPG image file format, the following example demonstrates how that is simple:

  1. Prepare a source Markdown document. In the example, we create a Markdown file from code.
  2. Prepare a path for converted file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method to save Markdown as an HTML document.
  4. Use the ConvertHTML() method to render the intermediary HTML document to JPG image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method for converting HTML to JPG.

If your case is to create a Markdown document from a user string directly in your code and convert it to a JPG file, the following example could help you:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5...
 6   // Prepare a path to a source Markdown file
 7   string sourcePath = Path.Combine(OutputDir, "document.md");
 8
 9   // Prepare a simple Markdown example
10   var code = "### Hello, World!" +
11              "\r\n" +
12              "[visit applications](https://products.aspose.app/html/applications)";
13   // Create a Markdown file
14   File.WriteAllText(sourcePath, code);
15
16   // Prepare a path for converted file saving 
17   string savePath = Path.Combine(OutputDir, "document-output.jpg");
18
19   // Convert Markdown to HTML document
20   using var document = Converter.ConvertMarkdown(sourcePath);
21
22   // Convert HTML document to JPG image file format
23   Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), savePath);

You can download the complete examples and data files from GitHub.

Convert Markdown to JPG using ImageSaveOptions

If your case is to convert an existing Markdown document from a local file system, the following example could help you. You need to follow a few steps:

  1. Open an existing Markdown document. In the example, we load a Markdown file from a local file system ( nature.md).
  2. Prepare a path for converted file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method of the Converter class to save Markdown as an HTML document.
  4. Create a new ImageSaveOptions object and specify the required properties.
  5. Use the ConvertHTML() method to render the intermediary HTML document to JPG image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method.

The following code snippet demonstrates how to convert Markdown to JPG using custom save options:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5using System.Drawing;
 6using Aspose.Html.Drawing;
 7using System.Drawing.Drawing2D;
 8...
 9    // Prepare a path to a source Markdown file
10    string sourcePath = Path.Combine(DataDir, "nature.md");
11
12    // Prepare a path for converted file saving 
13    string savePath = Path.Combine(OutputDir, "nature-options.jpg");
14
15    // Convert Markdown to HTML document
16    using var document = Converter.ConvertMarkdown(sourcePath);
17
18    // Initialize ImageSaveOptions 
19    var options = new ImageSaveOptions(ImageFormat.Jpeg)
20    {
21        SmoothingMode = SmoothingMode.HighQuality,
22        HorizontalResolution = 200,
23        VerticalResolution = 200,
24        BackgroundColor = System.Drawing.Color.AliceBlue
25    };
26    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 950), new Margin(30, 20, 10, 10));
27
28    // Convert HTML document to JPG image file format
29    Converter.ConvertHTML(document, options, savePath);

The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting Markdown to JPG format. To learn more about ImageSaveOptions, please read the Fine-Tuning Converters article.

In the above example, we use:

Convert Markdown to PNG in C#

The following code snippet demonstrates how to convert Markdown to PNG:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5...
 6   // Prepare a path to a source Markdown file
 7   string sourcePath = Path.Combine(DataDir, "document.md");
 8
 9   // Prepare a path for converted file saving 
10   string savePath = Path.Combine(OutputDir, "output.png");
11
12   // Convert Markdown to HTML document
13   using var document = Converter.ConvertMarkdown(sourcePath);
14
15   // Convert HTML document to PNG image file format
16   Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);

Aspose.HTML offers a free online MD to PNG Converter that converts Markdown to PNG image with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Convert Markdown to BMP in C#

The following code snippet demonstrates how to convert Markdown to BMP:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5...
 6   // Prepare a path to a source Markdown file
 7   string sourcePath = Path.Combine(DataDir, "document.md");
 8
 9   // Prepare a path for converted file saving 
10   string savePath = Path.Combine(OutputDir, "output.bmp");
11
12   // Convert Markdown to HTML document
13   using var document = Converter.ConvertMarkdown(sourcePath);
14
15   // Convert HTML document to BMP image file format
16   Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Bmp), savePath);

Convert Markdown to GIF in C#

The following code snippet demonstrates how to convert Markdown to GIF:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5...
 6   // Prepare a path to a source Markdown file
 7   string sourcePath = Path.Combine(DataDir, "document.md");
 8
 9   // Prepare a path for converted file saving 
10   string savePath = Path.Combine(OutputDir, "output.gif");
11
12   // Convert Markdown to HTML document
13   using var document = Converter.ConvertMarkdown(sourcePath);
14
15   // Convert HTML document to GIF image file format
16   Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Gif), savePath);

Convert Markdown to TIFF in C#

The following code snippet demonstrates how to convert Markdown to TIFF:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Saving;
 4using Aspose.Html.Rendering.Image;
 5...
 6   // Prepare a path to a source Markdown file
 7   string sourcePath = Path.Combine(DataDir, "document.md");
 8
 9   // Prepare a path for converted file saving 
10   string savePath = Path.Combine(OutputDir, "output.tiff");
11
12   // Convert Markdown to HTML document
13   using var document = Converter.ConvertMarkdown(sourcePath);
14
15   // Convert HTML document to TIFF image file format
16   Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Tiff), savePath);

Download our Aspose.HTML for .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

You can check the quality of Markdown to JPG conversion with our online MD to JPG Converter. Upload, convert your files and get results in a few seconds. Try our forceful Markdown to JPG Converter for free now!

Text “Banner MD to JPG Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.