Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how do I import C# Bitmaps
QtWS25 Last Chance

how do I import C# Bitmaps

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • lonnieL Offline
    lonnieL Offline
    lonnie
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Could you post the C# code for the save?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • lonnieL Offline
        lonnieL Offline
        lonnie
        wrote on last edited by lonnie
        #3

        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.

        1 Reply Last reply
        0
        • lonnieL Offline
          lonnieL Offline
          lonnie
          wrote on last edited by
          #4

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

          VRoninV 1 Reply Last reply
          1
          • lonnieL lonnie

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            @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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            lonnieL 1 Reply Last reply
            4
            • VRoninV VRonin

              @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

              lonnieL Offline
              lonnieL Offline
              lonnie
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved