0

I am doing something like this:

QImage image(width, height, QImage::Format_RGB32);
frame.fill(QColor(255, 255, 255).rgb());

QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);

Option 1:

out << image;

Option 2:

out.writeRawData((char *) image.constBits(), image.byteCount()) ;

Option 1 is pretty slow and I am not sure if Option 2 is the correct way to do?

surana4u
  • 31
  • 1
  • 3

2 Answers2

3

You can use QImage::save to write directly to a QIODevice, be it a buffer or a file.

image.save(buffer);

Option 2 looks pretty gross compared to Option 1; I would certainly prefer Option 1 aesthetically. But I would prefer the API I mentioned over both the options you give.

You can read more about image read/write here.

George Hilliard
  • 15,402
  • 9
  • 58
  • 96
1

I have just done something similar

QByteArray byteArray;
QBuffer buffer(&byteArray);
image.save(&buffer, "PNG"); // writes the image in PNG format inside the buffer

To output the buffer I would use a QString, I used this for converting to base64

QString imgBase64 = QString::fromLatin1(byteArray.toBase64().data()); 
John
  • 71
  • 1
  • 8