Search code examples
c++imagebase64fltk

Display base64 encoded image in FLTK


Is it possible to display images that have been base64 encoded as strings in FLTK? I want to avoid having an extra image file outside the executable if possible. I might be able to package the image with the executable, but it seems easier just to base64 encode it inside the source code. I might be mistaken, though.


Solution

  • A long while ago I also tried to embed images to my FLTK app, base64 was the obvious choice, unfortunately I can't recall what prevented me to use it.

    So I end up with a small utility which serializes PNG files to Fl_RGB_Image as byte array (this is my tweaked version, I can't remember where I found it, so I can't credit its original author).

    Compile it as you would any FLTK app, e.g. g++ -o "png2img" png2img.cpp -lfltk_images -lfltk -lpng (or see the Makefile at GitHub), then run it as ./png2img <path to folder with png files>. It'll generate two files: images.cpp and images.h, feel free to rename.

    Finally you can add images.cpp to your build chain and use images.h:

    #include "images.h" // check the available images
    // ...
    button_play = new Fl_Button();
    button_play->image(img_icon_play); // A Fl_RGB_Image from images.h