Struct Image#

Struct Documentation#

struct Image#

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);

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));

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] };
}

Public Functions

inline Image()#
inline Image(SharedPixelBuffer<Rgb8Pixel> buffer)#

Construct an image from a SharedPixelBuffer of RGB pixels.

inline Image(SharedPixelBuffer<Rgba8Pixel> buffer)#

Construct an image from a SharedPixelBuffer of RGBA pixels.

inline Size<unsigned int> size() const#

Returns the size of the Image in pixels.

inline std::optional<slint::SharedString> path() const#

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

Public Static Functions

static inline Image load_from_path(const SharedString &file_path)#

Load an image from an image file.

Friends

inline friend bool operator==(const Image &a, const Image &b)#

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

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

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