Convert Markdown to Image in C#

To convert Markdown to image formats in C#, first use Converter.ConvertMarkdown() to create an intermediate HTMLDocument, then render it with Converter.ConvertHTML() and ImageSaveOptions. Set the image format to JPG, PNG, BMP, GIF, or TIFF.

Markdown to image conversion is useful for previews, thumbnails, documentation snapshots, visual archives, and systems that need raster output instead of editable Markdown or HTML. In Aspose.HTML for .NET, Markdown is parsed to HTML first, then the intermediate HTMLDocument is rendered to the selected image format.

Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

Convert Markdown to JPG in C#

Use JPG output when you need compact previews or thumbnails and small file size matters more than transparency or lossless text edges. The example creates a Markdown source, converts it to an intermediate HTMLDocument, and renders it to JPG.

The workflow is:

  1. Prepare the Markdown source file.
  2. Convert Markdown to an HTMLDocument with Converter.ConvertMarkdown(sourcePath).
  3. Create an ImageSaveOptions instance with the required image format, such as ImageFormat.Jpeg.
  4. Call Converter.ConvertHTML(document, options, savePath) to render the intermediate HTML document as an image file.
 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);

Convert Markdown to JPG with ImageSaveOptions

Use ImageSaveOptions when the generated image needs custom rendering settings. The example below converts an existing Markdown file and applies image save options before rendering to JPG.

To customize Markdown to image conversion:

  1. Load or create the Markdown source file.
  2. Convert Markdown to an intermediate HTMLDocument with Converter.ConvertMarkdown().
  3. Create ImageSaveOptions and choose the output format.
  4. Configure only the options required by the target output, such as PageSetup, BackgroundColor, HorizontalResolution, VerticalResolution, UseAntialiasing, or TIFF Compression.
  5. Pass the HTMLDocument, ImageSaveOptions, and output path to Converter.ConvertHTML().
 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);
OptionUse it to control
FormatThe output image format. The default value is PNG; use ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Gif, or ImageFormat.Tiff when another format is required.
BackgroundColorThe color used to fill the image background. The default value is transparent.
PageSetupPage size, margins, and page layout settings.
HorizontalResolution and VerticalResolutionOutput resolution values used by the rendering process. The default value is 300 dpi for each property.
UseAntialiasingImage rendering quality. Antialiasing is enabled by default.
TextText rendering configuration through TextOptions.
CompressionTIFF compression. Use it only for TIFF output; the default value is LZW.

Use UseAntialiasing = true when you want to improve the visual quality of rendered shapes, text, and images, especially when clarity and smooth edges are important. Antialiasing can also increase processing time, so applications focused on rendering speed may choose UseAntialiasing = false.

Choose an Image Format for Markdown Output

Convert Markdown to PNG in C#

PNG is the default ImageSaveOptions.Format value and is often the best choice for sharp documentation previews. The following example converts Markdown to 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);

Convert Markdown to BMP in C#

BMP can be useful for Windows-oriented or legacy image workflows. The following example converts Markdown to 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);

Convert Markdown to GIF in C#

GIF can be useful for simple compatibility-focused output. The following example converts Markdown to 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);

Convert Markdown to TIFF in C#

TIFF can be useful for archival, printing, scanning, and document imaging workflows. The following example converts Markdown to 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);

Related Markdown Conversion Guides

Other Platforms

Try Online Markdown to Image Conversion

                
            

Use the online MD to JPG Converter for quick manual conversion. Use Aspose.HTML for .NET when Markdown to image conversion must run inside a C# application, service, or documentation pipeline.

MD to JPG Converter