Skip to content

slint::Image Struct

struct Image;
C++
#include <slint.h>
C++

An image type that can be displayed by the Image element

You can construct Image objects from a path to an image file on disk, using Image::load_from_path().

Another typical use-case is to render the image content with C++ code. For this it’s most efficient to create a new SharedPixelBuffer with the known dimensions and pass the pixel pointer returned by begin() to your rendering function. Afterwards you can create an Image using the constructor taking a SharedPixelBuffer.

The following example creates a 320x200 RGB pixel buffer and calls a function to draw a shape into it:

slint::SharedPixelBuffer<slint::Rgb8Pixel> pixel_buffer(320, 200);
low_level_render(pixel_buffer.width(), pixel_buffer.height(),
static_cast<unsigned char *>(pixel_buffer.begin()));
slint::Image image(pixel_buffer);
C++

Another use-case is to import existing image data into Slint, by creating a new Image through copying of the buffer:

slint::Image image(slint::SharedPixelBuffer<slint::Rgb8Pixel>(the_width, the_height,
static_cast<slint::Rgb8Pixel*>(the_data));
C++

This only works if the static_cast is valid and the underlying data has the same memory layout as slint::Rgb8Pixel or slint::Rgba8Pixel. Otherwise, you will have to do a pixel conversion as you copy the pixels:

slint::SharedPixelBuffer<slint::Rgb8Pixel> pixel_buffer(the_width, the_height);
slint::Rgb8Pixel *raw_data = pixel_buffer.begin();
for (int i = 0; i < the_width * the_height; i++) {
raw_data[i] = { bgr_data[i * 3 + 2], bgr_data[i * 3 + 1], bgr_data[i * 3] };
}
C++
enum class BorrowedOpenGLTextureOrigin
ValueDescription
TopLeftThe top-left of the texture is the top-left of the texture drawn on the screen.
BottomLeft

This enum describes the origin to use when rendering a borrowed OpenGL texture.

bool operator==(const Image &a, const Image &b)

Returns true if a refers to the same image as b; false otherwise.

bool operator!=(const Image &a, const Image &b)

Returns false if a refers to the same image as b; true otherwise.

slint::Image::Image()
slint::Image::Image(SharedPixelBuffer< Rgb8Pixel > buffer)

Construct an image from a SharedPixelBuffer of RGB pixels.

slint::Image::Image(SharedPixelBuffer< Rgba8Pixel > buffer)

Construct an image from a SharedPixelBuffer of RGBA pixels.

Size< uint32_t > slint::Image::size() const

Returns the size of the Image in pixels.

std::optional< slint::SharedString > slint::Image::path() const

Returns the path of the image on disk, if it was constructed via Image::load_from_path().

void slint::Image::set_nine_slice_edges(unsigned short top, unsigned short right, unsigned short bottom, unsigned short left)

Sets the nine-slice edges of the image.

Nine-slice scaling is a method for scaling images in such a way that the corners are not distorted. The arguments define the pixel sizes of the edges that cut the image into 9 slices.

std::optional< SharedPixelBuffer< Rgb8Pixel > > slint::Image::to_rgb8() const

Returns the pixel buffer for the Image if available in RGB format without alpha. Returns nullopt if the pixels cannot be obtained, for example when the image was created from borrowed OpenGL textures.

std::optional< SharedPixelBuffer< Rgba8Pixel > > slint::Image::to_rgba8() const

Returns the pixel buffer for the Image if available in RGBA format. Returns nullopt if the pixels cannot be obtained, for example when the image was created from borrowed OpenGL textures.

std::optional< SharedPixelBuffer< Rgba8Pixel > > slint::Image::to_rgba8_premultiplied() const

Returns the pixel buffer for the Image if available in RGBA format, with the alpha channel pre-multiplied to the red, green, and blue channels. Returns nullopt if the pixels cannot be obtained, for example when the image was created from borrowed OpenGL textures.

static Image slint::Image::load_from_path(const SharedString &file_path)

Load an image from an image file.

static Image slint::Image::create_from_borrowed_gl_2d_rgba_texture(uint32_t texture_id, Size< uint32_t > size, BorrowedOpenGLTextureOrigin origin=BorrowedOpenGLTextureOrigin::TopLeft)

Constructs a new Image from an existing OpenGL texture. The texture remains borrowed by Slint for the duration of being used for rendering, such as when assigned as source property to an Image element. It’s the application’s responsibility to delete the texture when it is not used anymore.

The texture must be bindable against the GL_TEXTURE_2D target, have GL_RGBA as format for the pixel data.

When Slint renders the texture, it assumes that the origin of the texture is at the top-left. This is different from the default OpenGL coordinate system. If you want to flip the origin, use BorrowedOpenGLTextureOrigin::BottomLeft.

Safety:

This function is unsafe because invalid texture ids may lead to undefined behavior in OpenGL drivers. A valid texture id is one that was created by the same OpenGL context that is current during any of the invocations of the callback set on [Window::set_rendering_notifier()]. OpenGL contexts between instances of [slint::Window] are not sharing resources. Consequently [slint::Image] objects created from borrowed OpenGL textures cannot be shared between different windows.


© 2026 SixtyFPS GmbH