SvgReaderSettings.ResolveImage Property

Gets or sets the callback that loads the images that the SVG file references.

Namespace: Aurigma.GraphicsMill.Codecs
Assembly: Aurigma.GraphicsMill (in Aurigma.GraphicsMill.dll)

Syntax

C#
public ResolveImageCallback ResolveImage { get; set; }

Property Value

A ResolveImageCallback delegate, or null. The default value is null, which loads the images from the folder of the SVG file.

Remarks

The reader calls this callback for every <image> element of the SVG file, except images embedded as data: URIs. Use it to load images that are stored somewhere else, for example, in another folder, in a database, or on a web server.

In the callback, set Image to a raster image or GraphicsContainer to vector content. If you set neither, the image is loaded from the folder of the SVG file.

If the callback throws an exception, the reader stops reading the file, and the SvgReader constructor throws this exception.

Examples

C#
const string assetsPrefix = "assets/";
const string assetsFolder = @"Images";

var settings = new SvgReaderSettings();

// The SVG file refers to images in the "assets" folder, which is stored somewhere else.
settings.ResolveImage = args =>
{
    if (args.Href.StartsWith(assetsPrefix, StringComparison.OrdinalIgnoreCase))
    {
        var fileName = System.IO.Path.Combine(assetsFolder, args.Href.Substring(assetsPrefix.Length));

        // The reader takes the image when the property is set, so you can dispose the bitmap right away.
        using (var bitmap = new Bitmap(fileName))
        {
            args.Image = bitmap;
        }
    }

    // If you set neither Image nor GraphicsContainer, the image is loaded from the folder of the SVG file.
};

using (var reader = new SvgReader(@"Images\SvgWithImage.svg", settings))
using (var writer = ImageWriter.Create(@"Images\Output\SvgWithImage.png"))
{
    Pipeline.Run(reader + writer);
}

The SVG file rendered with the image that the callback loads.

The SVG file rendered with the image that the callback loads.

See Also

Reference