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. Save QImage to Base64 String
QtWS25 Last Chance

Save QImage to Base64 String

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqbytearrayqbufferbase64
11 Posts 4 Posters 16.0k 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.
  • Q qDebug
    14 Oct 2016, 01:25

    Hi Guys!

    I've done some code to create an image and so far it works as expected. I can save it to (png) file. So far so good. But now i'd like to save the same image to a buffer and convert it to a base64 string. I tough this is an easy task but so far no luck.

    QByteArray ba;
    QBuffer bu(&ba);
    
    image.save(&bu, "PNG");
    
    QString imgBase64 = ba.toBase64();
    qDebug() << "imgBase64: " << imgBase64;
    
    

    The qdebug does not get printed. No errors. I have no clue why it isn't working.

    Any suggestions?

    Thanks!

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 14 Oct 2016, 04:17 last edited by
    #2

    @qDebug Not even "imgBase64: " is printed? You could try to debug and see what imgBase64 contains after assignment.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • Q Offline
      Q Offline
      qDebug
      wrote on 14 Oct 2016, 04:56 last edited by qDebug
      #3

      Nope, not even the string before. The whole line gets ignored somehow. If i place a qDebug before and after, both get printed. There is nothing to debug. No errors come up.

      I only gen an error in 5.7.0 (Windows MSVC), when i set "Terminal" as font

      DirectWrite: CreateFontFaceFromHDC() failed (...) for QFontDef(Family="Terminal", pointsize=12, pixelsize=16, styleHint=5, weight=50, stretch=50, hintingPreference=0) LOGFONT("Terminal", lfWidth=6, lfHeight=-16) dpi=96
      

      But if i use other fonts, the error is gone. And even with this error showing up, the app compiles and runs using terminal as font. That can't be related to by base64 problem, since i use other fonts without errors.

      J 1 Reply Last reply 14 Oct 2016, 06:24
      0
      • Q qDebug
        14 Oct 2016, 04:56

        Nope, not even the string before. The whole line gets ignored somehow. If i place a qDebug before and after, both get printed. There is nothing to debug. No errors come up.

        I only gen an error in 5.7.0 (Windows MSVC), when i set "Terminal" as font

        DirectWrite: CreateFontFaceFromHDC() failed (...) for QFontDef(Family="Terminal", pointsize=12, pixelsize=16, styleHint=5, weight=50, stretch=50, hintingPreference=0) LOGFONT("Terminal", lfWidth=6, lfHeight=-16) dpi=96
        

        But if i use other fonts, the error is gone. And even with this error showing up, the app compiles and runs using terminal as font. That can't be related to by base64 problem, since i use other fonts without errors.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 14 Oct 2016, 06:24 last edited by
        #4

        @qDebug You can try to print using std::cout.
        And you always can debug, not only if an error occurs: place a break point at this line:

        qDebug() << "imgBase64: " << imgBase64;
        

        and check the content of imgBase64.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • Q Offline
          Q Offline
          qDebug
          wrote on 14 Oct 2016, 06:35 last edited by qDebug
          #5

          I did set a breakpoint and run the debug, just without any results. As i mentioned before, the only debug error occurs if i use terminal font, the rest of the code shows no sign of problems.

          std::cout << imgBase64.toStdString();
          

          Shows the base64 string. Strange.

          J 1 Reply Last reply 14 Oct 2016, 08:32
          0
          • Q qDebug
            14 Oct 2016, 06:35

            I did set a breakpoint and run the debug, just without any results. As i mentioned before, the only debug error occurs if i use terminal font, the rest of the code shows no sign of problems.

            std::cout << imgBase64.toStdString();
            

            Shows the base64 string. Strange.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 14 Oct 2016, 08:32 last edited by
            #6

            @qDebug said in Save QImage to Base64 String:

            debug error

            Again: it is not about getting the error. If you have the breakpoint then your app should stop there if you run it through the debugger. Then you can inspect the values of your variables like imgBase64 to see whether they have expected values or not.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qDebug
              wrote on 14 Oct 2016, 09:04 last edited by qDebug
              #7

              Again, i did that. I did set a break point i did debug. There is nothing to debug, no relevant data shows up. If you don't believe me, i can take screen for ya ;)

              Can we end the talk about debugging please, it is getting boring :D

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 Oct 2016, 09:10 last edited by
                #8

                Hi,

                You don't open your buffer for writing.

                See the example code from the constructor your are using.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • Q Offline
                  Q Offline
                  qDebug
                  wrote on 14 Oct 2016, 22:06 last edited by
                  #9

                  I did try to open the buffer (and close it).

                  QByteArray ba;
                  QBuffer bu(&ba);
                  //bu.open(QBuffer::ReadWrite);
                  bu.open(QIODevice::WriteOnly);
                  image.save(&bu, "PNG");
                  //bu.close();
                  //QString imgBase64 = ba.toBase64();
                  QString imgBase64 = QString::fromLatin1(ba.toBase64().data());
                  qDebug() << "image base64: " << imgBase64;
                  

                  qDebug can't show the base64 string for some reason because

                  std::cout << imgBase64.toStdString();
                  

                  prints the correct base64 string. I did try serval version of code before i posted here. If i read the png from file and get a base64 from it, the same happens without the buffer. qDebug won't print out the base64 string.

                  QByteArray ba;
                  ba = file2.readAll().toBase64();
                  qDebug() << "base64 from png: " << ba.data(); // nothing
                  std::cout << ba.data(); // works
                  

                  I wonder why.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mohammad Sherafat
                    wrote on 14 Apr 2023, 18:27 last edited by
                    #10
                    QImageReader imageReader(fileName());
                    QImage image = imageReader.read();
                    
                    QByteArray ba;
                    QBuffer buffer(&ba);
                    buffer.open(QIODevice::WriteOnly);
                    image.save(&buffer, "PNG"); 
                    
                    QString base64 = QString(ba.toBase64(QByteArray::Base64Encoding));
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mohammad Sherafat
                      wrote on 14 Apr 2023, 19:34 last edited by Mohammad Sherafat
                      #11

                      This works

                       QBuffer buffer;
                      buffer.open(QIODevice::WriteOnly);
                      QImage qp(fileName());
                      qp.save(&buffer, "PNG");
                      QString base64 = buffer.data().toBase64();
                      

                      https://stackoverflow.com/questions/69165252/how-to-convert-png-image-into-base-64-format-in-qt

                      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