Build with XuiView Markdown

Images and SVG output

Fit an image into a view without stretching it, then export a vector-only view to SVG for a report or snapshot. Both use IContext, but image loading and file export belong to different environments.

Fit an image without distortion

using Xui.Core.Canvas;
using Xui.Core.DI;
using Xui.Core.Math2D;
using Xui.Core.UI;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class ImageCard(string assetUri) : View
{
    private IImage? image;
    protected override void OnActivate()
    {
        image = this.GetService<IImage>();
        image?.Load(assetUri); // Use a small packaged asset here.
        InvalidateRender();
    }

    protected override void OnDeactivate() => image = null;
    protected override void RenderCore(IContext context)
    {
        context.SetFill(new Color(0xFAF7FFFF));
        context.FillRect(Frame);
        if (image is null || image.Size.Width <= 0 || image.Size.Height <= 0)
            return;
        NFloat width = NFloat.Max(0, Frame.Width - 48);
        NFloat height = NFloat.Max(0, Frame.Height - 48);
        NFloat scale = NFloat.Min(width / image.Size.Width, height / image.Size.Height);
        var size = new Size(image.Size.Width * scale, image.Size.Height * scale);
        context.DrawImage(image, new Rect(Frame.Center.X - size.Width / 2, Frame.Center.Y - size.Height / 2, size.Width, size.Height));
    }
}

Construct ImageCard with the URI of a small packaged image, such as asset://test-512.png in DemoApp. In your own app, package the asset under the URI your runtime resolves; merely writing a path does not include that file in the application.

The view resolves an IImage when active. Its size may be empty until loading completes. The draw method checks both dimensions before dividing by them, so an unavailable image does not produce invalid geometry.

Take the smaller of the horizontal and vertical scales. Multiply both image dimensions by that same value, then center the destination rectangle. This is aspect fit: the entire image stays visible and unused space remains on two sides. For a crop-to-fill design, choose a source crop deliberately and use the DrawImage overload with source and destination rectangles.

Load is appropriate here for a small packaged asset. Browser loading still completes asynchronously and the browser image implementation invalidates the window when ready. For slow or remote content, use LoadAsync, handle failure, and apply results on the UI thread only while the intended page is active. Do not block on a task from a render callback.

The IImage interface does not itself inherit IDisposable. The sample releases its reference on deactivation; the runtime owns its catalog and cleanup policy. If you acquire an explicitly owned disposable resource through another API, dispose it according to that contract rather than assuming every image handle has identical ownership.

Export a vector view

Use a .NET tool or test project with references to Xui.Runtime.Software and Xui.Core.Fonts in addition to Core. This helper writes a 400 by 600 SVG:

using System.IO;
using Xui.Core.Fonts;
using Xui.Core.UI;
using Xui.Runtime.Software;

public static class SvgExport
{
    public static void Save(View view, string path)
    {
        var document = new SVGDocument
        {
            Content = view,
            Size = (400, 600),
            FontUris = Inter.URIs
        };
        File.WriteAllText(path, document.ToSVG());
    }
}

Pass a freshly constructed, vector-only view, for example the meter from Create a View subclass. SVGDocument measures, arranges, and renders it without opening a native window. It does not activate the view, create platform services, dispatch input, or run animation. Set the intended state before exporting.

FontUris supplies Inter for measurement. The default SVG font resolver treats the font as externally available; use a custom SvgFontResolver when the recipient needs embedded or linked fonts for identical text presentation.

Keep image and GPU limitations explicit

A native or browser image handle is not portable into the SVG backend. Its image drawing requires a loaded image from SoftwareImagePipeline. A bare SVGDocument does not provide the activation/services used by ImageCard, so exporting that card unchanged produces its background, not the loaded photo.

Likewise, a GPU surface is not automatically converted to vector paths. Export the supported Canvas content or capture an appropriate raster result. Choose the output format according to what the renderer can actually reproduce.