Save QImage to Base64 String
-
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!
-
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.
-
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.
-
Hi,
You don't open your buffer for writing.
-
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.
-
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));
-
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