Skip to content
Pavel edited this page Oct 19, 2026 · 7 revisions

English | Русский

Images are drawn inside the text and load in the background. While an image loads, and if it fails, its alt text is shown in its place.

Syntax and size

Any .md file:

![Logo](images/logo.png "Tooltip of the image")

![A narrow logo](images/logo.png){width=120}

The logo in a line of text: <img src="images/logo.png" alt="Logo" width="120" height="40">

[![A logo that is a link](images/logo.png)](https://github.com/porohkun/Beresta.Markdown.Wpf)

Without a size an image takes its size in pixels, like in a browser, and shrinks to the width of the text if it is wider. A size in attributes, {width=120 height=40}, or in HTML is in pixels, px allowed; with only one side given, the other keeps the proportions. The title is a tooltip, and a linked image opens its link like a text link does.

An <img> tag on its own line, <a href><img></a> and <picture> are images too, and <p align="center"> centers them — see How It Renders → HTML from READMEs.

Where images come from

The default loader reads:

  • http:// and https:// addresses;
  • files: absolute paths and file:// addresses;
  • WPF resources: pack://application:,,,/YourApp;component/images/logo.png;
  • data:image/png;base64,… addresses.

A relative address is resolved against BaseUri, the address of the document. For a document opened from a file, it is the file itself.

MainWindow.xaml.cs, opening a file (using System.IO;):

private void Open(string path)
{
    Viewer.BaseUri = new Uri(Path.GetFullPath(path));
    Viewer.Markdown = File.ReadAllText(path);
}

Which images load at all is decided by ImagePolicy: by default everything but files on other computers — see Untrusted Text.

Images are decoded by WPF: PNG, JPEG, BMP, GIF (without animation), TIFF, ICO. A format WPF cannot decode, such as SVG, shows the alt text — or plug in a loader that can. A large image is decoded no wider than the screen, or than its width from the Markdown.

The default loader reads at most MdImageLoader.MaxDownloadBytes — 20 MB by default — from the network, a file or data:: a larger image shows its alt text. It introduces itself to servers in User-Agent as Beresta.Markdown.Wpf/<version>.

App.xaml.cs, at the start of the application (using Beresta.Markdown.Wpf;):

MdImageLoader.MaxDownloadBytes = 5 * 1024 * 1024;

The cache

Loaded images are kept within MdImageCache.Budget — 128 MB by default — and the long unused ones leave first, so a block that scrolls back into view or survives a new text shows its image at once. An image that failed to load is tried again the next time it is shown, and a load that no image waits for anymore — the block has left the screen, the window is closed — is canceled.

When an image changes where it lives — a file is saved anew, a server has a new picture — drop it: MdImageCache.Invalidate(address) takes the address the way the viewer resolved it against BaseUri, MdImageCache.Clear() drops every image. The viewers that show it load it again.

MainWindow.xaml.cs, when a picture file is saved (using Beresta.Markdown.Wpf;):

private void OnPictureSaved(string path) => MdImageCache.Invalidate(new Uri(path));

Your own loader

IMdImageLoader has one method: by the address from the Markdown (url), the same address resolved against BaseUri (uri) and the width in pixels to decode the image to at most (decodeWidth, 0 — full size), it returns a frozen ImageSource, or null to show the alt text. A loader that throws shows the alt text too. Its token is canceled when no image waits for the load anymore. A loader of your own serves images from a database, an archive, an authorized server, or adds formats; MdImageLoader.Decode(bytes, decodeWidth) decodes bytes the way the default loader does.

Pictures by name from the attachments folder of the application: ![Chart](attachment:chart.png).

AttachmentImageLoader.cs:

using System.IO;
using System.Windows.Media;
using Beresta.Markdown.Wpf;

public sealed class AttachmentImageLoader : IMdImageLoader
{
    private const string Scheme = "attachment:";

    public async Task<ImageSource?> LoadAsync(string url, Uri? uri, int decodeWidth, CancellationToken cancellationToken)
    {
        if (!url.StartsWith(Scheme, StringComparison.Ordinal))
        {
            return await MdImageLoader.Instance.LoadAsync(url, uri, decodeWidth, cancellationToken);
        }

        var path = Path.Combine(AppContext.BaseDirectory, "attachments", Path.GetFileName(url[Scheme.Length..]));
        if (!File.Exists(path))
        {
            return null;
        }

        var bytes = await File.ReadAllBytesAsync(path, cancellationToken);
        return await Task.Run(() => MdImageLoader.Decode(bytes, decodeWidth), cancellationToken);
    }
}

MainWindow.xaml.cs, in the constructor after InitializeComponent():

Viewer.ImageLoader = new AttachmentImageLoader();

ImageLoader="{x:Null}" turns images off: every image shows its alt text.

The look

The alt text color is the key Md.Image.AltForeground, the whole control is the style Md.Image.Style, and the ImageTemplate of a style provider replaces it altogether — see Styles and Templates. Screen readers name an image by its alt text — see Accessibility.

Beresta.Markdown.Wpf

Basics

Working with Text

Appearance

Integration

Editing

Your Own Syntax

Ready-made Extensions


По-русски

Основы

Работа с текстом

Оформление

Встраивание в приложение

Правка

Свой синтаксис

Готовые расширения

Clone this wiki locally