Skip to content

ui::ImageSource

Header: winui/render/ImageSource.hpp.

What an Image element shows: a still Bitmap, an AnimatedImage or an SvgImage, held by value.

Converts implicitly from each of the three, so .source = photo works whichever photo is. Copying an ImageSource copies a still picture's pixels, so build one once and move it in. load() instead decodes a file on a background thread: the source is empty while it loads, and an Image showing it lays itself out again and raises on_image_opened (or on_image_failed) once the decode is done.

Example:

ui::Bitmap       photo = ui::Bitmap::read("photo.jpg", {.max_width = 800});
ui::ImageSource gif   = ui::AnimatedImage::read("spinner.gif");
ui::ImageSource logo  = ui::SvgImage::read("logo.svg");

window.set_content(ui::VerticalStack{
    .spacing  = 8,
    .content  = {
        ui::Image{.source = std::move(photo), .stretch = ui::Stretch::UniformToFill, .height = 200},
        ui::Image{.source = gif},
        ui::Image{.source = logo, .width = 64, .height = 64},
    },
});

ImageSource

ImageSource() = default

An empty source; an Image showing it draws nothing.

ImageSource

ImageSource(Bitmap image)

A still picture.

ImageSource

ImageSource(AnimatedImage image)

An animated picture, which the Image element plays.

ImageSource

ImageSource(SvgImage svg)

An SVG document.

load_state

ImageLoadState load_state() const

Loading or Failed for a load() still decoding or one that failed; Loaded otherwise.

load_error

std::string load_error() const

Why a load() failed; empty unless load_state() is Failed.

empty

bool empty() const

True when there is no content, or the content itself is empty (zero-size or no frames).

natural_size

Size natural_size() const

The content's own size in DIPs, one pixel to a DIP for a bitmap: what an Image with Stretch None draws at.

bitmap

const Bitmap* bitmap() const

The still picture, or nullptr when the source is something else (or still loading).

animated

const AnimatedImage* animated() const

The animated picture, or nullptr when the source is something else (or still loading).

svg

const SvgImage* svg() const

The SVG document, or nullptr when the source is something else (or still loading).

load

static ImageSource load(std::filesystem::path path)

Decodes the picture at path on a background thread, choosing the decoder by its extension: an SVG document for .svg, an animated image for .gif, a still picture otherwise. Copies share the decode.

Example:

ui::Image{.source = ui::ImageSource::load("photos/cliff.jpg"), .stretch = ui::Stretch::UniformToFill}