Image in Markdown via C#

Images can be added to any Markdown file using Markdown Syntax. In this article, you learn about adding images in Markdown, showing images as links, and adding altText to Markdown images using C#. Aspose.HTML for .NET library is a stand-alone solution that allows you to parse and manipulate Markdown files without using other software. You can create a Markdown document from scratch, open from a local file and manipulate by inserting new elements, removing, or editing the content of existing nodes.
Use the Aspose.Html.Toolkit.Markdown.Syntax and Aspose.Html.Toolkit.Markdown.Parser namespaces to provide full Markdown parsing and MD syntax tree management based on the GitHub Flavored Markdown (GFM) specification.


How to Add Image in Markdown using C#

Using the C# library, you can programmatically edit MD files – change the document structure and content. Let’s look at creating a new Markdown document from scratch and adding image to it.

C# code to add an image to MD file

    // Create the MarkdownSyntaxTree
    var md = new MarkdownSyntaxTree(new Configuration());

    // Create a Markdown syntax factory
    var mdf = md.SyntaxFactory;

    // Specify an image altText
    var altText = "The San Juan Mountains are beautiful";

    // Specify an image label
    var label = "San Juan Mountains";

    // Specify the path to an image
    var href = "C:/temp/image.png";

    // Create an Image element:
    var image = mdf.InlineImage(altText, href, label);

    // Add the image into MD document
    md.AppendChild(image);

    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "markdown-image.md");

    // Save MD file
    md.Save(savePath);



Steps to Add an Image to MD File in C#

A few simple steps to create Markdown Image:

  1. Create the MarkdownSyntaxTree using MarkdownSyntaxTree() constructer.
  2. Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree.
  3. Specify an image altText, label and path to the image and pass them to InlineImage().
  4. Create an Image element using InlineImage() constructor in SyntaxFactory.
  5. Call the AppendChild() method of the MarkdownSyntaxNode class to add the image into MD syntax tree.

Add a Markdown Image as a Link in C#

The following C# code example shows how to add an image as a link into MD document:

C# code to create a Markdown image as a link

    // Create the MarkdownSyntaxTree
    var markdown = new MarkdownSyntaxTree(new Configuration());

    // Create a link node and add it to Markdown syntax tree
    var link = markdown.SyntaxFactory.InlineLink("", "https://docs.aspose.com/html/", "");
    markdown.AppendChild(link);

    // Markdown Image Link – create an image node and add it as a link
    var image = markdown.SyntaxFactory.InlineImage("Aspose.HTML for .NET", "https://products.aspose.com/html/images/headers/aspose_html-for-net.svg", "C# HTML Parser – Documentation");
    link.AppendChild(image);
            
    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "markdown-image-link.md");

    // Save MD file
    markdown.Save(savePath);

Documentation

To learn more about Aspose.HTML API, please visit our documentation guide. Markdown is a simple markup language that allows you to format plain text. The Markdown Syntax documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.

System Requirements

Our APIs are supported on all major platforms and Operating Systems. Before executing the code, please ensure that you have the following prerequisites on your system.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms.
  • Development environment like Microsoft Visual Studio.
  • Aspose.Html for .NET DLL referenced in your project – Install from NuGet using the Download button above.