how do I import C# Bitmaps

  • I have a database that contains some Bitmaps that were stored by C#. They are in base64 encoded strings. I have decoded them into a calloc space. Here is the code:

    int width = std::stoi (itm.width.toStdString() );
    int height = std::stoi (itm.height.toStdString());
    void * img64 = calloc(, 1);
    int len64 = Base64::Decode (,, (unsigned char *) img64 );

         QPushButton *button = new QPushButton(tr("Button"));
         QPixmap pixmap = QPixmap::fromImage(
                 (unsigned char *) img64,
         QIcon img(pixmap);
         button->setIconSize(QSize(width, height));

    The object of the exercise is to show the image to a button. It does not crash but just shows gibberish. I have tried all the available Format_RGBxxx formats and not really any difference for the end result.

    Any ideas for getting alien images into QT?

    Do I need the precise width and height of the original image? The width and height being used is the display size in the data form while the stored image could be much higher or lower. C# "just handled" it for me.

  • Could you post the C# code for the save?

  • Sorry I did not dig a bit deeper before bothering you guys.

    I read the decoded base64 string into a QVector so I could easily look at it in the Debugger. The data is basically the raw file, ie it has a PNG, JPG, BMP etc header.

    So now I am going to deal with it based on the raw file type. Problem is not solved, but at least I know what to be looking for and it explains why the Pixmap is gibberish.

  • Problem solved and VERY EASILY too.

    I'm getting the hang of this QT (and C++ again). I used to spend most of my time getting C# to do simple C++ things. I actually think of the problem in Assembler and translate to the higher level language I am using. The beauty of C# was the GUI controls but from what I have seen the QT GUI controls are more than adequate. The C++ base gives a huge speed increase and size decrease.
    QByteArray by = QByteArray::fromBase64(;
    QImage image = QImage::fromData(by, "PNG");
    QLabel label;

  • @lonnie said in how do I import C# Bitmaps:

    This is useless overhead:

    • if is QString use
    • if is QByteArray use directly

    QImage image = QImage::fromData(by, "PNG");

    this is duplication. you can use

    QPixmap image;

    If you are not sure the format is always PNG you can use QImageReader autodetection capabilities.

    QPixmap::fromImage has an rvalue reference overload you could have used in this case: label.setPixmap(QPixmap::fromImage(std::move(image))); but, as I said, It's better to go the loadFromData way

  • @VRonin said in how do I import C# Bitmaps:

    if is QString use

    That is so much cleaner. Thanks.

    The code to avoid the duplication works perfectly and is easier to understand as well.

    QPixmap image;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.