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(itm.data.length(), 1);
    int len64 = Base64::Decode (itm.data.toStdString(), itm.data.length(), (unsigned char *) img64 );

         QPushButton *button = new QPushButton(tr("Button"));
    
         QPixmap pixmap = QPixmap::fromImage(
             QImage(
                 (unsigned char *) img64,
                 width,
                 height,
                 QImage::Format_RGB888
             )
         );
    
         QIcon img(pixmap);
         button->setIcon(img);
         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.

    http://www.qtcentre.org/threads/25186-Encode-decode-base-64-images
    QByteArray by = QByteArray::fromBase64(itm.data.toStdString().c_str());
    QImage image = QImage::fromData(by, "PNG");
    QLabel label;
    label.setPixmap(QPixmap::fromImage(image));
    label.show();



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

    itm.data.toStdString().c_str()

    This is useless overhead:

    • if itm.data is QString use itm.data.toLatin1()
    • if itm.data is QByteArray use itm.data directly

    QImage image = QImage::fromData(by, "PNG");
    label.setPixmap(QPixmap::fromImage(image));

    this is duplication. you can use

    QPixmap image;
    image.loadFromData(by,"PNG");
    label.setPixmap(image);
    

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

    P.S.
    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 itm.data is QString use itm.data.toLatin1()

    That is so much cleaner. Thanks.

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

    QPixmap image;
    image.loadFromData(by,"PNG");
    label.setPixmap(image);


Log in to reply
 

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