Question

when i resize a QPixmap with scaledToHeight and then transform it to an QByteArray, the size of this ByteArray is exactly the size of ByteArray from the unscaled QPixmap. Is there a possibility to shrink QPixmap in pixel-size and file-size?

Best regards

Was it helpful?

Solution

Here is a proof-of-concept that shows that what you are doing is certainly possible. Maybe you can see what you are doing different?

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QPixmap pixmap("/path/to/image.jpg");
  QByteArray bytes1;
  QBuffer buffer1(&bytes1);
  buffer1.open(QIODevice::WriteOnly);
  pixmap.save(&buffer1, "png");
  qDebug() << bytes1.size();

  pixmap = pixmap.scaledToHeight(100);
  QByteArray bytes2;
  QBuffer buffer2(&bytes2);
  buffer2.open(QIODevice::WriteOnly);
  pixmap.save(&buffer2, "png");
  qDebug() << bytes2.size();

  return app.exec();
}

Two guesses:

  • scaledToHeight returns a scaled copy of the original image. It does not scale the pixmap instance directly.
  • If you are reusing the same QByteArray, you may have to truncate it before seeing the difference in size. That is, the capacity of the QByteArray may remain larger even through the actual content is much smaller.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top