Save QImage to Base64 String
-
wrote on 14 Oct 2016, 01:25 last edited by
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!
-
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!
@qDebug Not even "imgBase64: " is printed? You could try to debug and see what imgBase64 contains after assignment.
-
wrote on 14 Oct 2016, 04:56 last edited by qDebug
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.
-
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.
@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.
-
wrote on 14 Oct 2016, 06:35 last edited by qDebug
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.
-
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.
@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.
-
wrote on 14 Oct 2016, 09:04 last edited by qDebug
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
-
Hi,
You don't open your buffer for writing.
-
wrote on 14 Oct 2016, 22:06 last edited by
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.
-
wrote on 14 Apr 2023, 18:27 last edited by
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));
-
wrote on 14 Apr 2023, 19:34 last edited by Mohammad Sherafat
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